参考:https://studygolang.com/articles/10530
- sort.Sort或sort.Stable
- 实现sort.Interface 接口
参考:
https://learnku.com/articles/38269
package main
import (
"fmt"
"sort"
)
func main() {
type Person struct {
Name string
Age int
}
a := []Person{
{"D",1},
{"E",2},
{"F",3},
{"A",3},
{"B",2},
{"C",1},
}
sort.SliceStable(a, func(i, j int) bool {
return a[i].Age < a[j].Age || (a[i].Age == a[j].Age && a[i].Name < a[j].Name)
})
for _,v := range a{
fmt.Println(v)
}
}
参考:https://books.studygolang.com/The-Golang-Standard-Library-by-Example/chapter03/03.1.html