// An example of sorting `int`s. ints := []int{7, 2, 4} sort.Ints(ints) fmt.Println("Ints: ", ints)
// 检查slice是否是排序状态 s := sort.IntsAreSorted(ints) fmt.Println("Sorted: ", s) }
2、自定义排序
实现 sort.Interface 的三个方法,Len,Swap,Less。
1 2 3 4 5 6 7 8 9
type Interface interface { // Len is the number of elements in the collection. Len() int // Less reports whether the element with // index i should sort before the element with index j. Less(i, j int) bool // Swap swaps the elements with indexes i and j. Swap(i, j int) }