sort包
sort.Interface
type Inerface interface {
Len() int //Len方法返回集合中的元素个数
Less(i, j int) bool //i > j,该方法返回索引i的元素是否比索引j的元素小,返回值为真时执行Swap函数
SWap(i, j int) //交换i,j的值
}
func Ints(a []int)
func IntsAreSorted(a []int) bool
func SearchInts(a []int, x int) int
func Float64s(a []float64)
func Float64sAreSorted(a []float64) bool
func SearchFloat64s(a []float64, x float64) int
func SearchFloat64s(a []float64, x float64) bool
func Strings(a []string)
func StringsAreSorted(a []string) bool
func SearchStrings(a []string, x string) int
func Sort(data Interface)
func Stable(data Interface)
func Reverse(data Interface) Interface
func ISSorted(data Interface) bool
func Search(n int, f func(int) bool) int
func (p xxxSlice) Len() int
func (p xxxSlice) Less(i, j int) bool
func (p xxxSlice) Swap(i, j int)
func (p xxxSlice) Search(x xxx) int
func (p xxxSlice) Sort()
对内置数据类型的排序
切片的升序排序
Ints()Float64s()Strings()intfloat64string
package main
import (
"fmt"
"sort"
)
func main() {
intSlice := []int{3, 1, 2, 5, 4}
float64Slice := []float64{3.2, 1.0, 2.1, 5.4, 4.3}
stringSlice := []string{"abcd", "aacd", "bcda", "d"}
sort.Ints(intSlice) //1, 2, 3, 4, 5
sort.Float64s(float64Slice) //1.0, 2.1, 3.2, 4.3, 5.4
sort.Strings(stringSlice) //"aacd", "abcd", "bcda", "d"
}
切片的降序排序
- go的cmp法则,是通过Sort函数实现的,即Less与Swap函数搭配使用
package main
import (
"fmt"
"sort"
)
func main() {
intSlice := []int{3, 1, 2, 5, 4}
float64Slice := []float64{3.2, 1.0, 2.1, 5.4, 4.3}
stringSlice := []string{"abcd", "aacd", "bcda", "d"}
sort.Sort(sort.Reverse(sort.IntSlice(intSlice)))
//5, 4, 3, 2, 1
sort.Sort(sort.Reverse(sort.Float64Slice(float64Slice)))
//5.4, 4.3, 3.2, 2.1, 1.0
sort.Sort(sort.Reverse(sort.StringSlice(stringSlice)))
//"d", "bcda", "abcd", "aacd"
}
map的value排序
package main
import (
"fmt"
"sort"
)
type mapSlice []map[string]int
func (ms mapSlice) Len() int {
return len(ms)
}
func (ms mapSlice) Swap(i, j int) {
ms[i], ms[j] = ms[j], ms[i]
}
func (ms mapSlice) Less(i, j int) bool {
if ms[i]["a"] != ms[j]["a"] {
return ms[i]["a"] < ms[j]["a"]
} else {
return ms[i]["b"] < ms[j]["b"]
}
}
func main() {
ms := mapSlice{
{"a":4, "b":12},
{"a":3, "b":11},
{"a":3, "b":10},
}
fmt.Println(ms)
//[map[a:4 b:12] map[a:3 b:11] map[a:3 b:10]]
fmt.Println("------")
sort.Sort(ms)
fmt.Println(ms)
//[map[a:3 b:10] map[a:3 b:11] map[a:4 b:12]]
}
自定义数据类型排序
结构体排序方法 1
//定义自定义结构体的切片
//type 切片名字 []自定义类型名字
type newSlice []structName
//定义Len()、Swap()和Less()三个函数
func (this newSlice) Len() int {
return len(this)
}
func (this newSlice) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}
func (this newSlice) Less(i, j int) bool {
return this[i].Value < this[j].Value
//Value假设是要比较的元素,此是选用‘小于’符号进行比较,就相当于是升序排序
}
package main
import (
"fmt"
"sort"
)
type Person struct {
Name string
Age int
}
type PersonSlice []Person
func (p PersonSlice) Len() int {
return len(p)
}
func (p PersonSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p PersonSlice) Less(i, j int) bool {
return p[i].Age > p[j].Age
}
func main() {
people := []Person{
{"zhang san", 12},
{"li si", 30},
{"wang wu", 52},
{"zhao liu", 26},
}
fmt.Println(people)
//[{zhang san 12} {li si 30} {wang wu 52} {zhao liu 26}]
fmt.Println("---------")
sort.Sort(PersonSlice(people))
fmt.Println(people)
//[{wang wu 52} {li si 30} {zhao liu 26} {zhang san 12}]
fmt.Println("---------")
sort.Sort(sort.Reverse(PersonSlice(people)))
//[{zhang san 12} {zhao liu 26} {li si 30} {wang wu 52}]
fmt.Println(people)
}
结构体排序方法2
- 第二种方法是提供Less的接口,可以每次排序的时候,给出想要的排序规则的函数,而不必重写Less函数
package main
import (
"fmt"
"sort"
)
type Person struct {
Name string
Age int
}
type PersonWrapper struct {
people []Person
by func(p, q *Person) bool
}
func (pw PersonWrapper) Len() int {
return len(pw.people)
}
func (pw PersonWrapper) Swap(i, j int) {
pw.people[i], pw.people[j] = pw.people[j], pw.people[i]
}
func (pw PersonWrapper) Less(i, j int) bool {
return pw.by(&pw.people[i], &pw.people[j])
}
func main() {
people := []Person{
{"zhangsan", 12},
{"lisi", 30},
{"wangwu", 52},
{"zhaoliu", 26},
}
fmt.Println(people)
//[{zhangsan 12} {lisi 30} {wangwu 52} {zhaoliu 26}]
fmt.Println("----------")
sort.Sort(PersonWrapper{people, func(p, q *Person) bool {
return p.Age > q.Age //Age降序排序
}})
fmt.Println(people)
//[{wangwu 52} {lisi 30} {zhaoliu 26} {zhangsan 12}]
fmt.Println("----------")
sort.Sort(PersonWrapper{people, func(p, q *Person) bool {
return p.Name < q.Name //Name升序排序
}})
fmt.Println(people)
//[{lisi 30} {wangwu 52} {zhangsan 12} {zhaoliu 26}]
fmt.Println("---------")
}
通过Slice()函数实现结构体类型的排序
package main
import (
"fmy"
"sort"
)
type newStruct struct {
Id int
Name string
}
type newSlice []newStruct
func main() {
mySlice := make(newSLice, 0)
for i := 0; i < 10; i++ {
mySlice = append(mySlice, newSLice {
Id : i,
Name : fmt.Sprintf("slice_%d", i),
})
}
sort.Slice(mySlice, func(i, j int) bool { return mySlice[i].Id < mySlice[j].Id })
//这样是按Id进行升序排序
}