参考:
https://segmentfault.com/a/1190000016514382 (值得一看)
Go 常见排序方式
整数、浮点数、字符串切片排序
sort
包提供了一下几种排序函数:
sort.Ints(x []int)
sort.Float64s(x []float64)
sort.Strings(x []string)
使用自定义比较器进行排序
- 使用
sort.Slice(x interfacec{}, less func(i, j int) bool)
进行排序(快速排序 + 堆排序 + 希尔排序),其中less()
为自定义的排序逻辑(使用匿名函数定义排序规则);
x := [][]int{
{
2,5},{
4,7},{
1,5},{
7,9}}
sort.Slice(x, func(i,</