golang編程開發(fā)使用sort排序示例詳解
golang sort package: https://studygolang.com/articles/3360
sort 操作的對(duì)象通常是一個(gè) slice,需要滿足三個(gè)基本的接口,并且能夠使用整數(shù)來(lái)索引
// A type, typically a collection, that satisfies sort.Interface can be // sorted by the routines in this package. The methods require that the // elements of the collection be enumerated by an integer index. 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) }
ex-1 對(duì) []int 從小到大排序
package main import ( "fmt" "sort" ) type IntSlice []int func (s IntSlice) Len() int { return len(s) } func (s IntSlice) Swap(i, j int){ s[i], s[j] = s[j], s[i] } func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] } func main() { a := []int{4,3,2,1,5,9,8,7,6} sort.Sort(IntSlice(a)) fmt.Println("After sorted: ", a) }
ex-2 使用 sort.Ints 和 sort.Strings
golang 對(duì)常見的 []int
和 []string
分別定義了 IntSlice
和 StringSlice
, 實(shí)現(xiàn)了各自的排序接口。而 sort.Ints 和 sort.Strings 可以直接對(duì) []int 和 []string 進(jìn)行排序, 使用起來(lái)非常方便
package main import ( "fmt" "sort" ) func main() { a := []int{3, 5, 4, -1, 9, 11, -14} sort.Ints(a) fmt.Println(a) ss := []string{"surface", "ipad", "mac pro", "mac air", "think pad", "idea pad"} sort.Strings(ss) fmt.Println(ss) sort.Sort(sort.Reverse(sort.StringSlice(ss))) fmt.Printf("After reverse: %v\n", ss) }
ex-3 使用 sort.Reverse 進(jìn)行逆序排序
如果我們想對(duì)一個(gè) sortable object 進(jìn)行逆序排序,可以自定義一個(gè)type。但 sort.Reverse 幫你省掉了這些代碼
package main import ( "fmt" "sort" ) func main() { a := []int{4,3,2,1,5,9,8,7,6} sort.Sort(sort.Reverse(sort.IntSlice(a))) fmt.Println("After reversed: ", a) }
ex-4 使用 sort.Stable 進(jìn)行穩(wěn)定排序
sort.Sort 并不保證排序的穩(wěn)定性。如果有需要, 可以使用 sort.Stable
package main import ( "fmt" "sort" ) type person struct { Name string Age int } type personSlice []person func (s personSlice) Len() int { return len(s) } func (s personSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s personSlice) Less(i, j int) bool { return s[i].Age < s[j].Age } func main() { a := personSlice { { Name: "AAA", Age: 55, }, { Name: "BBB", Age: 22, }, { Name: "CCC", Age: 0, }, { Name: "DDD", Age: 22, }, { Name: "EEE", Age: 11, }, } sort.Stable(a) fmt.Println(a) }
以上就是go語(yǔ)言編程使用sort來(lái)排序示例詳解的詳細(xì)內(nèi)容,更多關(guān)于go語(yǔ)言sort排序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go語(yǔ)言實(shí)現(xiàn)二維數(shù)組的2種遍歷方式以及案例詳解
這篇文章主要介紹了Go語(yǔ)言實(shí)現(xiàn)二維數(shù)組的2種遍歷方式以及案例詳解,圖文代碼聲情并茂,有感興趣的可以學(xué)習(xí)下2021-03-03Go語(yǔ)言實(shí)現(xiàn)開發(fā)一個(gè)簡(jiǎn)單的gRPC Demo
這篇文章主要為大家詳細(xì)介紹了如何利用Go語(yǔ)言實(shí)現(xiàn)開發(fā)一個(gè)簡(jiǎn)單的gRPC Demo,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-07-07Go?gRPC進(jìn)階教程gRPC轉(zhuǎn)換HTTP
這篇文章主要為大家介紹了Go?gRPC進(jìn)階教程gRPC轉(zhuǎn)換HTTP教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06利用Golang生成整數(shù)隨機(jī)數(shù)方法示例
這篇文章主要介紹了利用Golang生成整數(shù)隨機(jī)數(shù)的相關(guān)資料,文中給出了詳細(xì)的介紹和完整的示例代碼,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04關(guān)于golang中死鎖的思考與學(xué)習(xí)
本文主要介紹了關(guān)于golang中死鎖的思考與學(xué)習(xí),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03golang通過gorm操作sqlite設(shè)置主鍵自增的步驟
這篇文章主要介紹了golang通過gorm操作sqlite設(shè)置主鍵自增的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09