sort
最后更新于:2022-04-02 02:45:58
[TOC]
## 语法
```
func Float64s(a []float64)
func Float64sAreSorted(a []float64) bool
func Ints(a []int)
func IntsAreSorted(a []int) bool
func Strings(a []string)
func StringsAreSorted(a []string) bool
func IsSorted(data Interface) bool
func Search(n int, f func(int) bool) int
func SearchFloat64s(a []float64, x float64) int
func SearchInts(a []int, x int) int
func SearchStrings(a []string, x string) int
func Slice(slice interface{}, less func(i, j int) bool)
func SliceIsSorted(slice interface{}, less func(i, j int) bool) bool
func SliceStable(slice interface{}, less func(i, j int) bool)
func Sort(data Interface)
func Stable(data Interface)
```
> Stable排序并保证排序的稳定性,相等元素的相对次序不变。
## 示例
### sort.Ints 排序
```
ints := []int{5, 4, 2, 1, 1, 3, 4}
sort.Ints(ints)
fmt.Printf("%+v\n", ints)
```
### sort.IntsAreSorted 是否排序
```
sort.IntsAreSorted(ints) // true
```
### sort.Reverse 倒排
```
ints := []int{5, 4, 2, 1, 1, 3, 4}
sort.Sort(sort.Reverse(sort.IntSlice(ints)))
fmt.Printf("%+v\n", ints) // [5 4 4 3 2 1 1]
```
### 结构体排序
#### 方式一: sort.Slice 直接使用
```
people := []struct {
Name string
Age int
}{
{"Gopher", 7},
{"Alice", 55},
{"Vera", 24},
{"Bob", 75},
}
sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name })
fmt.Println("By name:", people)
// By name: [{Alice 55} {Bob 75} {Gopher 7} {Vera 24}]
```
#### 方式二: sort.Slice 绑定结构体
#### 方式三: 实现 sort.Interface 接口
### Search 对已排序进行二分查找 数组查找 ``` a := []int{1, 2, 3, 4, 5, 6} fmt.Printf("%+v\n", sort.SearchInts(a, 5)) // 4 ``` sort.Search 查询 ``` num := []int{2, 3, 5, 7, 8, 22, 100} index := sort.Search(len(num), func(i int) bool { return num[i] > 21 }) fmt.Printf("%+v\n", num[index]) // 22 ``` 自定义结构查找 ``` people := []struct { Name string Age int }{ {"Gopher", 7}, {"Vera", 24}, {"Alice", 55}, {"Bob", 75}, } // 实际效果为满足条件的第一个index index := sort.Search(len(people), func(i int) bool { // 等24 返回不正确, 需要使用 >=24 return people[i].Age >= 24 }) fmt.Printf("%+v\n", people[index]) // {Name:Vera Age:24} ```
';
main.go
``` type People struct { Name string Age int } type Peoples []People func (p Peoples) sort() { sort.Slice(p, func(i, j int) bool { return p[i].Name < p[j].Name }) } func main() { people := Peoples{ {"Gopher", 7}, {"Alice", 55}, {"Vera", 24}, {"Bob", 75}, } people.sort() fmt.Printf("%+v\n", people) // [{Name:Vera Age:24} {Name:Gopher Age:7} {Name:Bob Age:75} {Name:Alice Age:55}] } ```#### 方式三: 实现 sort.Interface 接口
main.go
``` package main import ( "fmt" "sort" ) //学生成绩结构体 type StuScore struct { name string //姓名 score int //成绩 } type StuScores []StuScore func (s StuScores) Len() int { return len(s) } func (s StuScores) Less(i, j int) bool { return s[i].score < s[j].score } func (s StuScores) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func main() { stus := StuScores{ {"alan", 95}, {"hikerell", 91}, {"acmfly", 96}, {"leao", 90}, } sort.Sort(stus) //打印排序后的stus数据 fmt.Println("Sorted:\n\t", stus) } ```### Search 对已排序进行二分查找 数组查找 ``` a := []int{1, 2, 3, 4, 5, 6} fmt.Printf("%+v\n", sort.SearchInts(a, 5)) // 4 ``` sort.Search 查询 ``` num := []int{2, 3, 5, 7, 8, 22, 100} index := sort.Search(len(num), func(i int) bool { return num[i] > 21 }) fmt.Printf("%+v\n", num[index]) // 22 ``` 自定义结构查找 ``` people := []struct { Name string Age int }{ {"Gopher", 7}, {"Vera", 24}, {"Alice", 55}, {"Bob", 75}, } // 实际效果为满足条件的第一个index index := sort.Search(len(people), func(i int) bool { // 等24 返回不正确, 需要使用 >=24 return people[i].Age >= 24 }) fmt.Printf("%+v\n", people[index]) // {Name:Vera Age:24} ```