自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go 語言中排序的三種方法

開發(fā) 后端
使用 Sort.Slice? 方法排序時,可以自定義比較函數(shù) less(i, j int) bool,這樣就可以根據(jù)需要按不同的字段進行排序。如果想要穩(wěn)定排序的話,就使用 Sort.SliceStable 方法。

在寫代碼過程中,排序是經(jīng)常會遇到的需求,本文會介紹三種常用的方法。

廢話不多說,下面正文開始。

使用標準庫

根據(jù)場景直接使用標準庫中的方法,比如:

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

舉個例子:

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

自定義比較器

使用 sort.Slice 方法排序時,可以自定義比較函數(shù) less(i, j int) bool,這樣就可以根據(jù)需要按不同的字段進行排序。

如果想要穩(wěn)定排序的話,就使用 sort.SliceStable 方法。

舉個例子:

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 方法,它們可以對任意實現(xiàn)了 sort.Interface 的數(shù)據(jù)結(jié)構(gòu)排序。

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

意思就是說,只要某一個數(shù)據(jù)結(jié)構(gòu)實現(xiàn)了 Len() int,Less(i, j int) bool 和 Swap(i, j int) 這三個方法,那么就可以使用 sort.Sort 來排序。

舉個例子:

type Person struct {
    Name string
    Age  int
}

// ByAge implements sort.Interface based on the Age field.
type ByAge []Person

func (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}]
}

字典排序

我們都知道,字典是無序的,具體原因可以看之前寫的這篇文章 Go 語言 map 如何順序讀???

如果想要字典按 key 或者 value 排序的話,可以這樣做。

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

以上就是本文的全部內(nèi)容。

參考文章:

  • https://yourbasic.org/golang/how-to-sort-in-go/#performance-and-implementation
責(zé)任編輯:姜華 來源: yongxinz
相關(guān)推薦

2020-08-12 08:51:19

Go語言Concurrency后臺

2021-12-20 07:11:26

Java List排序 Java 基礎(chǔ)

2019-09-09 15:13:19

Yum包管理器Linux

2022-05-31 16:00:46

Go 編程語言復(fù)制文件Go 標準庫

2009-07-08 12:56:32

編寫Servlet

2020-03-13 18:10:08

Linuxapt軟件包

2011-06-10 10:43:12

Ubuntu應(yīng)用安裝

2009-06-23 10:45:18

Hibernate支持

2009-12-11 18:49:39

預(yù)算編制博科資訊

2024-11-15 07:00:00

Python發(fā)送郵件

2022-07-13 16:06:16

Python參數(shù)代碼

2011-04-18 15:32:45

游戲測試測試方法軟件測試

2023-08-14 17:58:13

RequestHTTP請求

2010-09-14 15:10:49

CSS注釋

2020-06-17 10:52:00

DDoS攻擊網(wǎng)絡(luò)攻擊網(wǎng)絡(luò)安全

2016-10-12 13:53:38

JavaByteBufferRandomAcces

2010-09-08 13:29:48

CSS

2023-02-21 14:58:12

間序列周期數(shù)據(jù)集

2010-11-16 16:11:28

Oracle身份驗證

2021-07-13 12:31:27

IT組織改進首席技術(shù)官
點贊
收藏

51CTO技術(shù)棧公眾號