Go–基础----sort模块
sort模块对各类型进行排序:
package main
import (
"fmt"
"sort"
)
type User struct {
ID int
Nmae string
Score float64
}
func main() {
//数组
list := [][2]int{{1, 3}, {5, 9}, {4, 5}, {6, 5}, {5, 8}}
//排序-使用数组的第二个元素比较大小进行排
sort.Slice(list, func(i, j int) bool {
return list[i][1] > list[j][1]
})
fmt.Println(list)
users := []User{{1, "kk", 8}, {2, "烟灰", 9}, {3, "第三代", 6}, {4, "阿强", 2}}
sort.Slice(users, func(i, j int) bool {
return users[i].Score < users[j].Score
})
fmt.Println(users)
}