Go語(yǔ)言sort包函數(shù)使用示例
sort包簡(jiǎn)介
官方文檔Golang的sort包用來(lái)排序,二分查找等操作。本文主要介紹sort包里常用的函數(shù),通過(guò)實(shí)例代碼來(lái)快速學(xué)會(huì)使用sort包
sort包內(nèi)置函數(shù)
sort.Ints(x []int)
ints := []int{1, 4, 3, 2}
fmt.Printf("%v\n", ints)
sort.Ints(ints) //默認(rèn)升序
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)
- 使用方法同上,都是對(duì)內(nèi)置int string float64類型的便捷排序
sort.Slice(x any, less func(i, j int) bool)
- 傳入對(duì)象是切片,要自己實(shí)現(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]
- 同時(shí)也可以對(duì)結(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)
- 自定義排序,需要實(shí)現(xiàn) Len() Less() Swap() 三個(gè)方法
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ù)是用來(lái)二分查找的, 默認(rèn)是在左邊插入
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ù)需要自己實(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,通過(guò)自定義條件便實(shí)現(xiàn)了相等情況下在右邊插入,前者默認(rèn)是在左邊
- 更高級(jí)一點(diǎn)的用法
mysring := []string{"abcd", "bcde", "bfag", "cddd"}
idx := sort.Search(len(mysring), func(i int) bool {
// 查找頭兩位字母不是b的,,返回找到的第一個(gè)
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 {
//查找第一個(gè)字母不是b的
return mysring[i][0] <= byte('b')
})
fmt.Printf("%v\n", mysring[idx]) // abcd以上就是Go語(yǔ)言sort包使用示例的詳細(xì)內(nèi)容,更多關(guān)于Go語(yǔ)言sort包的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go語(yǔ)言leetcode題解953驗(yàn)證外星語(yǔ)詞典示例詳解
這篇文章主要為大家介紹了Go語(yǔ)言leetcode題解953驗(yàn)證外星語(yǔ)詞典示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Go語(yǔ)言中關(guān)于set的實(shí)現(xiàn)思考分析
Go?開發(fā)過(guò)程中有時(shí)我們需要集合(set)這種容器,但?Go?本身未內(nèi)置這種數(shù)據(jù)容器,故常常我們需要自己實(shí)現(xiàn),下面我們就來(lái)看看具體有哪些實(shí)現(xiàn)方法吧2024-01-01
用GO實(shí)現(xiàn)IP門禁優(yōu)化網(wǎng)絡(luò)流量管理
這篇文章主要為大家介紹了用GO實(shí)現(xiàn)IP門禁優(yōu)化網(wǎng)絡(luò)流量管理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Golang 統(tǒng)計(jì)字符串字?jǐn)?shù)的方法示例
本篇文章主要介紹了Golang 統(tǒng)計(jì)字符串字?jǐn)?shù)的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
k8s容器互聯(lián)flannel?vxlan通信原理
這篇文章主要為大家介紹了k8s容器互聯(lián)flannel?vxlan通信原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
詳解Go多協(xié)程并發(fā)環(huán)境下的錯(cuò)誤處理
這篇文章主要介紹了詳解Go多協(xié)程并發(fā)環(huán)境下的錯(cuò)誤處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

