1.4.1 一维数组的排序
在Go语言中,是要实现了
sort.Interface()接口,即可通过sort包内的的函数完成排序、查找等操作sort包已经吧[]int、[]float64、[]string3种类型都实现了该接口,可以方便调用Go语言中,
sort.Sort()函数是递增排序,sort.Reverse()是递减函数示例:
func main() { a := []int{4, 3, 2, 1, 5, 6} sort.Sort(sort.Reverse(sort.IntSlice(a))) fmt.Println("排序后:", a) sort.Sort(sort.IntSlice(a)) fmt.Println("排序后:", a) }结果:
排序后: [6 5 4 3 2 1] 排序后: [1 2 3 4 5 6]
Last updated