網站首頁 編程語言 正文
sort包簡介
官方文檔Golang的sort包用來排序,二分查找等操作。本文主要介紹sort包里常用的函數,通過實例代碼來快速學會使用sort包
sort包內置函數
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)
- 使用方法同上,都是對內置int string float64類型的便捷排序
sort.Slice(x any, less func(i, j int) bool)
- 傳入對象是切片,要自己實現回調函數
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]
- 同時也可以對結構體自定義排序規則
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)
- 自定義排序,需要實現?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
- 該函數是用來二分查找的, 默認是在左邊插入
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
- 這兩函數功能同上
sort.Search(n int, f func(int) bool) int
- 自定義的二分查找,回調函數需要自己實現查找條件
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,通過自定義條件便實現了相等情況下在右邊插入,前者默認是在左邊
- 更高級一點的用法
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
原文鏈接:https://www.cnblogs.com/notomatoes/p/16277688.html
相關推薦
- 2023-02-12 Jupyter?Notebook讀取csv文件出現的問題及解決_python
- 2023-12-07 com.fasterxml.jackson.databind.ObjectMapper
- 2022-02-18 Redis - Redis command timed out nested exception i
- 2023-01-10 Go語言defer與return執行的先后順序詳解_Golang
- 2022-12-14 C++?Boost?PointerContainer智能指針詳解_C 語言
- 2023-01-20 C#實現自定義動畫鼠標的示例詳解_C#教程
- 2023-03-01 getopts解析shell腳本命令行參數的方法_linux shell
- 2022-10-16 Python使用random.shuffle()隨機打亂字典排序_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支