sort
在之前的map中介绍过如何顺序遍历map,其中用到了sort包, sort包提供了排序切片和用户自定义数据集的函数。 在这里介绍一下sort包中常用的方法。
//Sort排序data。它调用1次data.Len确定长度,调用O(n*log(n))次data.Less和data.Swap。本函数不能保证排序的稳定性(即不保证相等元素的相对次序不变)。
func Sort(data Interface)
//Stable排序data,并保证排序的稳定性,相等元素的相对次序不变。
//它调用1次data.Len,O(n*log(n))次data.Less和O(n*log(n)*log(n))次data.Swap。
func Stable(data Interface)
//IsSorted报告data是否已经被排序。
func IsSorted(data Interface) bool
//Reverse包装一个Interface接口并返回一个新的Interface接口,对该接口排序可生成递减序列。
func Reverse(data Interface) Interface
s := []int{
5, 2, 6, 3, 1, 4} // unsorted
sort.Sort(sort.Reverse(sort.IntSlice(s)))
fmt.Println(s)//[6 5 4 3 2 1]
//Ints函数将a排序为递增顺序。
func Ints(a []int)
s := []int{
5