1:二维数组按照第一列大小排序
import (
"fmt"
"sort"
)
func main() {
interval := [][]int{
{2,3},
{2,2},
{3,3},
{1,3},
{5,7},
{2,2},
{4,6},
}
sort.Slice(interval, func(i, j int) bool {
return interval[i][0] < interval[j][0]//按照每行的第一个元素排序
})
fmt.Println(interval)
}
2:第一列相同时,按照第二列大小排序
func main() {
interval := [][]int{
{2,3},
{2,2},
{3,3},
{1,3},
{5,7},
{2,2},
{4,6},
}
sort.Slice(interval, func(i, j int) bool {
if interval[i][0] == interval[j][0] {
return interval[i][1] < interval[j][1]
}else {
return interval[i][0] < interval[j][0]
}
})
fmt.Println(interval)
}
结构体字段排序:https://blog.csdn.net/weixin_37078439/article/details/89202527