Go語言sort包函數(shù)使用示例
sort包簡介
官方文檔Golang的sort包用來排序,二分查找等操作。本文主要介紹sort包里常用的函數(shù),通過實例代碼來快速學會使用sort包
sort包內(nèi)置函數(shù)
sort.Ints(x []int)
ints := []int{1, 4, 3, 2} fmt.Printf("%v\n", ints) sort.Ints(ints) //默認升序 fmt.Printf("%v\n", ints) //[1 2 3 4] sort.Sort(sort.Reverse(sort.IntSlice(ints))) //降序排序 fmt.Printf("%v\n", ints) //[4 3 2 1]
sort.Strings(x []string)
sort.Float64s(x []float64)
- 使用方法同上,都是對內(nèi)置int string float64類型的便捷排序
sort.Slice(x any, less func(i, j int) bool)
- 傳入對象是切片,要自己實現(xiàn)回調(diào)函數(shù)
slices := []int{1, 1, 4, 5, 1, 4} sort.Slice(slices, func(i, j int) bool { return slices[i] < slices[j] }) fmt.Printf("%v\n", slices)//[1 1 1 4 4 5]
- 同時也可以對結(jié)構(gòu)體自定義排序規(guī)則
type stu struct { name string age int } stus := []stu{{"h", 20}, {"a", 23}, {"h", 21}} sort.Slice(stus, func(i, j int) bool { if stus[i].name == stus[j].name { return stus[i].age > stus[j].age // 年齡逆序 } return stus[i].name < stus[j].name // 名字正序 }) fmt.Printf("%v\n", stus) //[{a 23} {h 21} {h 20}]
sort.Sort(data Interface)
- 自定義排序,需要實現(xiàn) Len() Less() Swap() 三個方法
type Interface interface { // Len is the number of elements in the collection. Len() int // Less reports whether the element with // index i should sort before the element with index j. Less(i, j int) bool // Swap swaps the elements with indexes i and j. Swap(i, j int) }
- 使用代碼
type stu struct { name string age int } type student []stu func (s student) Len() int { return len(s) } func (s student) Less(i, j int) bool { if s[i].name == s[j].name { return s[i].age > s[j].age // 年齡逆序 } return s[i].name < s[j].name // 名字正序 } func (s student) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func main() { stus1 := student{{"h", 20}, {"a", 23}, {"h", 21}} sort.Sort(stus1) fmt.Printf("%v\n", stus1) //[{a 23} {h 21} {h 20}] 使用效果等同于sort.Slice }
- 使用效果等同于sort.Slice后者代碼量較少
sort.SearchInts(a []int, x int) int
- 該函數(shù)是用來二分查找的, 默認是在左邊插入
arr := []int{1, 2, 3, 4, 5, 6, 7} idx := sort.SearchInts(arr, 4) fmt.Printf("%v\n", idx) // 3
sort.SearchFloat64s(a []float64, x float64) int
sort.SearchStrings(a []string, x string) int
- 這兩函數(shù)功能同上
sort.Search(n int, f func(int) bool) int
- 自定義的二分查找,回調(diào)函數(shù)需要自己實現(xiàn)查找條件
arr := []int{1, 2, 3, 4, 5, 6, 7} idx := sort.Search(len(arr), func(i int) bool { return arr[i] > 4 }) fmt.Printf("%v\n", idx) //4
- 相比SearchInts,通過自定義條件便實現(xiàn)了相等情況下在右邊插入,前者默認是在左邊
- 更高級一點的用法
mysring := []string{"abcd", "bcde", "bfag", "cddd"} idx := sort.Search(len(mysring), func(i int) bool { // 查找頭兩位字母不是b的,,返回找到的第一個 return mysring[i][0] != 'b' && mysring[i][1] != 'b' }) fmt.Printf("%v\n", mysring[idx]) // cddd mysring := []string{"abcd", "bcde", "bfag", "cddd"} idx := sort.Search(len(mysring), func(i int) bool { //查找第一個字母不是b的 return mysring[i][0] <= byte('b') }) fmt.Printf("%v\n", mysring[idx]) // abcd
以上就是Go語言sort包使用示例的詳細內(nèi)容,更多關(guān)于Go語言sort包的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用GO實現(xiàn)IP門禁優(yōu)化網(wǎng)絡(luò)流量管理
這篇文章主要為大家介紹了用GO實現(xiàn)IP門禁優(yōu)化網(wǎng)絡(luò)流量管理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12Golang 統(tǒng)計字符串字數(shù)的方法示例
本篇文章主要介紹了Golang 統(tǒng)計字符串字數(shù)的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05k8s容器互聯(lián)flannel?vxlan通信原理
這篇文章主要為大家介紹了k8s容器互聯(lián)flannel?vxlan通信原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04詳解Go多協(xié)程并發(fā)環(huán)境下的錯誤處理
這篇文章主要介紹了詳解Go多協(xié)程并發(fā)環(huán)境下的錯誤處理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08