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ǔ)言interface接口繼承多態(tài)示例及定義解析
這篇文章主要為大家介紹了go語(yǔ)言interface接口繼承多態(tài)示例及定義解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
一文掌握Go語(yǔ)言并發(fā)編程必備的Mutex互斥鎖
Go 語(yǔ)言提供了 sync 包,其中包括 Mutex 互斥鎖、RWMutex 讀寫鎖等同步機(jī)制,本篇博客將著重介紹 Mutex 互斥鎖的基本原理,需要的可以參考一下2023-04-04
4個(gè)場(chǎng)景教會(huì)你Go中Goroutine和通道是怎么用的
本篇給出了4個(gè)在運(yùn)維開(kāi)發(fā)工作中較為常見(jiàn)的且也是比較典型的場(chǎng)景,通過(guò)這些場(chǎng)景來(lái)帶大家掌握Go中Goroutine和通道是怎么使用的,需要的可以學(xué)習(xí)一下2023-05-05

