日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁(yè) > 科技  > 軟件

Go 語(yǔ)言中排序的三種方法

來(lái)源: 責(zé)編: 時(shí)間:2023-08-20 23:17:15 3430觀看
導(dǎo)讀在寫代碼過(guò)程中,排序是經(jīng)常會(huì)遇到的需求,本文會(huì)介紹三種常用的方法。廢話不多說(shuō),下面正文開始。使用標(biāo)準(zhǔn)庫(kù)根據(jù)場(chǎng)景直接使用標(biāo)準(zhǔn)庫(kù)中的方法,比如:sort.Intssort.Float64ssort.Strings舉個(gè)例子:s := []int{4, 2, 3, 1}sort.I

1BX28資訊網(wǎng)——每日最新資訊28at.com

在寫代碼過(guò)程中,排序是經(jīng)常會(huì)遇到的需求,本文會(huì)介紹三種常用的方法。1BX28資訊網(wǎng)——每日最新資訊28at.com

廢話不多說(shuō),下面正文開始。1BX28資訊網(wǎng)——每日最新資訊28at.com

使用標(biāo)準(zhǔn)庫(kù)

根據(jù)場(chǎng)景直接使用標(biāo)準(zhǔn)庫(kù)中的方法,比如:1BX28資訊網(wǎng)——每日最新資訊28at.com

  • sort.Ints
  • sort.Float64s
  • sort.Strings

舉個(gè)例子:1BX28資訊網(wǎng)——每日最新資訊28at.com

s := []int{4, 2, 3, 1}sort.Ints(s)fmt.Println(s) // [1 2 3 4]

自定義比較器

使用 sort.Slice 方法排序時(shí),可以自定義比較函數(shù) less(i, j int) bool,這樣就可以根據(jù)需要按不同的字段進(jìn)行排序。1BX28資訊網(wǎng)——每日最新資訊28at.com

如果想要穩(wěn)定排序的話,就使用 sort.SliceStable 方法。1BX28資訊網(wǎng)——每日最新資訊28at.com

舉個(gè)例子:1BX28資訊網(wǎng)——每日最新資訊28at.com

family := []struct {    Name string    Age  int}{    {"Alice", 23},    {"David", 2},    {"Eve", 2},    {"Bob", 25},}// Sort by age, keeping original order or equal elements.sort.SliceStable(family, func(i, j int) bool {    return family[i].Age < family[j].Age})fmt.Println(family) // [{David 2} {Eve 2} {Alice 23} {Bob 25}]

自定義數(shù)據(jù)結(jié)構(gòu)

使用 sort.Sort 或者 sort.Stable 方法,它們可以對(duì)任意實(shí)現(xiàn)了 sort.Interface 的數(shù)據(jù)結(jié)構(gòu)排序。1BX28資訊網(wǎng)——每日最新資訊28at.com

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)}

意思就是說(shuō),只要某一個(gè)數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)了 Len() int,Less(i, j int) bool 和 Swap(i, j int) 這三個(gè)方法,那么就可以使用 sort.Sort 來(lái)排序。1BX28資訊網(wǎng)——每日最新資訊28at.com

舉個(gè)例子:1BX28資訊網(wǎng)——每日最新資訊28at.com

type Person struct {    Name string    Age  int}// ByAge implements sort.Interface based on the Age field.type ByAge []Personfunc (a ByAge) Len() int           { return len(a) }func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }func (a ByAge) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }func main() {    family := []Person{        {"Alice", 23},        {"Eve", 2},        {"Bob", 25},    }    sort.Sort(ByAge(family))    fmt.Println(family) // [{Eve 2} {Alice 23} {Bob 25}]}

字典排序

我們都知道,字典是無(wú)序的,具體原因可以看之前寫的這篇文章 Go 語(yǔ)言 map 如何順序讀取?1BX28資訊網(wǎng)——每日最新資訊28at.com

如果想要字典按 key 或者 value 排序的話,可以這樣做。1BX28資訊網(wǎng)——每日最新資訊28at.com

m := map[string]int{"Alice": 2, "Cecil": 1, "Bob": 3}keys := make([]string, 0, len(m))for k := range m {    keys = append(keys, k)}sort.Strings(keys)for _, k := range keys {    fmt.Println(k, m[k])}// Output:// Alice 2// Bob 3// Cecil 1

以上就是本文的全部?jī)?nèi)容。1BX28資訊網(wǎng)——每日最新資訊28at.com

參考文章:

  • https://yourbasic.org/golang/how-to-sort-in-go/#performance-and-implementation

本文鏈接:http://www.www897cc.com/showinfo-26-6191-0.htmlGo 語(yǔ)言中排序的三種方法

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: SpringBoot中的敏感信息的配置進(jìn)行加密處理,這種方式你知道嗎?

下一篇: H5-Dooring可視化頁(yè)面制作神器測(cè)評(píng)總結(jié)

標(biāo)簽:
  • 熱門焦點(diǎn)
Top 主站蜘蛛池模板: 霸州市| 迁西县| 永春县| 通州市| 福州市| 连州市| 集贤县| 罗城| 油尖旺区| 上犹县| 乐亭县| 拜泉县| 清远市| 思茅市| 白银市| 腾冲县| 股票| 凌云县| 通渭县| 拜城县| 汾阳市| 尚义县| 改则县| 宣汉县| 平武县| 胶州市| 广昌县| 阜新市| 乐都县| 南漳县| 奉化市| 巨鹿县| 黎川县| 墨玉县| 巴林右旗| 綦江县| 保德县| 平度市| 江孜县| 齐齐哈尔市| 澜沧|