关于go:在切片内对整数切片进行排序

sorting slice of integer inside a slices

我尝试在一个切片内对int切片进行排序,例如:

1
2
3
4
5
6
7
8
slices := make([][]int32, 0)
slices = append(slices, []int32{10,22})
slices = append(slices, []int32{13,22})
slices = append(slices, []int32{12,22})
// [[10 22] [13 22] [12 22]]
// to become
// [[10 22] [12 22] [13 22]] (ascending)
// *by the first element in slice

我不知道,但我想在检查后追加和提前


您只需要使用排序包中的切片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main

import (
   "fmt"
   "sort"
)
func main() {
    a := [][]int{[]int{10,3}, []int{5,12}, []int{5, 3}, []int{1,1}}
    fmt.Printf("before: %v
", a)
    sort.Slice(a, func(i,j int) bool{ return a[i][0] < a[j][0]})
    fmt.Printf("after: %v
", a)
}

为了使用稳定的排序,请改为sort.slicestablish。