1.4.1 一维数组的排序

  1. 在Go语言中,是要实现了sort.Interface()接口,即可通过sort包内的的函数完成排序、查找等操作

  2. sort包已经吧[]int[]float64[]string3种类型都实现了该接口,可以方便调用

  3. Go语言中,sort.Sort()函数是递增排序,sort.Reverse()是递减函数

  4. 示例:

    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