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標(biāo)準(zhǔn)庫(kù)http與fasthttp服務(wù)端性能對(duì)比場(chǎng)景分析
這篇文章主要介紹了Go標(biāo)準(zhǔn)庫(kù)http與fasthttp服務(wù)端性能比較,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
Go?Excelize?API源碼解析GetSheetFormatPr使用示例
這篇文章主要為大家介紹了Go?Excelize?API源碼解析GetSheetFormatPr使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Golang內(nèi)存泄漏場(chǎng)景以及解決方案詳析
golang中內(nèi)存泄露的發(fā)現(xiàn)與排查一直是來(lái)是go開發(fā)者頭疼的一件事,下面這篇文章主要給大家介紹了關(guān)于Golang內(nèi)存泄漏場(chǎng)景以及解決的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
GO語(yǔ)言開發(fā)終端命令行小工具改進(jìn)更新
這篇文章主要為大家介紹了GO語(yǔ)言開發(fā)終端命令行小工具的改進(jìn)更新,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Go1.21新增內(nèi)置函數(shù)(built-in?functions)詳解
Go?1.21新增的內(nèi)置函數(shù)分別是?min、max?和?clear,這篇文章主要帶大家一起了解一下這幾個(gè)函數(shù)的用途和使用示例,感興趣的小伙伴可以學(xué)習(xí)一下2023-08-08
一篇文章帶你搞懂Go語(yǔ)言標(biāo)準(zhǔn)庫(kù)Time
在我們開發(fā)的過(guò)程中,每個(gè)項(xiàng)目都需要時(shí)間這一類的函數(shù),此時(shí)對(duì)time這個(gè)包的研究深度就顯得尤為重要,這篇文章主要給大家介紹了關(guān)于如何通過(guò)一篇文章帶你搞懂Go語(yǔ)言標(biāo)準(zhǔn)庫(kù)Time的相關(guān)資料,需要的朋友可以參考下2022-10-10
go開發(fā)中引用靜態(tài)庫(kù).a文件的方法
這篇文章主要介紹了go開發(fā)中引用靜態(tài)庫(kù).a文件的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Go語(yǔ)言特點(diǎn)及基本數(shù)據(jù)類型使用詳解
這篇文章主要為大家介紹了Go語(yǔ)言特點(diǎn)及基本數(shù)據(jù)類型使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03

