Golang 語言怎么高效排序數(shù)據(jù)?
01介紹
在 Golang 語言項(xiàng)目開發(fā)中,經(jīng)常會(huì)遇到數(shù)據(jù)排序問題。Golang 語言標(biāo)準(zhǔn)庫 sort 包,為我們提供了數(shù)據(jù)排序的功能,我們可以直接使用 sort.Sort() 函數(shù)進(jìn)行數(shù)據(jù)排序,sort.Sort() 函數(shù)底層實(shí)現(xiàn)是以快排為主,并根據(jù)目標(biāo)數(shù)據(jù)的具體情況選擇不同的排序算法。本文我們介紹 sort 包排序數(shù)據(jù)的使用方法。
02切片排序
在 Golang 語言標(biāo)準(zhǔn)庫 sort 包中,sort.Sort() 函數(shù)用于數(shù)據(jù)排序,該函數(shù)需要一個(gè) interface 類型的入?yún)? sort.Interface,它包含三個(gè)方法,分別是 Len(),Less() 和 Swap()。也就是說,如果我們需要使用 sort 包的 Sort 函數(shù)進(jìn)行數(shù)據(jù)排序,首先入?yún)⒌臄?shù)據(jù)需要實(shí)現(xiàn)這三個(gè)方法,或者理解為任意元素類型的切片實(shí)現(xiàn)了這三個(gè)方法,都可以使用 sort.Sort() 函數(shù)排序數(shù)據(jù)。
sort 包代碼:
- type Interface interface {
- Len() int // 集合中元素的數(shù)量
- Less(i, j int) bool // 描述元素的順序
- Swap(i, j int) // 交換索引為 i 和 j 的元素
- }
- func Sort(data Interface)
需要注意的是 sort.Sort() 函數(shù)不能保證數(shù)據(jù)排序是穩(wěn)定的,如果需要保證數(shù)據(jù)排序穩(wěn)定,可以使用 sort.Stable() 函數(shù),“穩(wěn)定”的含義是原始數(shù)據(jù)中 a 和 b 的值相等,排序前 a 排在 b 的前面,排序后 a 仍排在 b 的前面。
為了方便讀者朋友們理解,我們使用 int 類型的切片作為示例,介紹 sort.Sort() 函數(shù)的使用方法,我們定義一個(gè)類型 type IntSlice []int,并且給類型 IntSlice 實(shí)現(xiàn) sort.Interface 接口類型定義的三個(gè)方法,然后使用 sort.Sort() 函數(shù)排序數(shù)據(jù)。
示例代碼:
- package main
- import (
- "fmt"
- "sort"
- )
- type IntSlice []int
- func (s IntSlice) Len() int {
- return len(s)
- }
- func (s IntSlice) Less(i, j int) bool {
- return s[i] > s[j]
- }
- func (s IntSlice) Swap(i, j int) {
- s[i], s[j] = s[j], s[i]
- }
- func main () {
- intSlice := IntSlice([]int{1, 3, 5, 7, 9})
- fmt.Println(intSlice) // 排序前
- sort.Sort(intSlice)
- fmt.Println(intSlice) // 排序后
- }
輸出結(jié)構(gòu):
- [9 7 5 3 1]
- [1 3 5 7 9]
讀到這里,我相信聰明的讀者朋友們已經(jīng)了解了 sort.Sort() 的使用方式,同時(shí)也會(huì)產(chǎn)生一個(gè)疑問,難道每次使用 sort.Sort() 排序數(shù)據(jù),都需要這么麻煩嗎?我還不如自己寫個(gè)遍歷排序數(shù)據(jù)。
是的,當(dāng)然不用這么麻煩,sort 包已經(jīng)幫我們封裝好了常用函數(shù),我們直接使用就可以了。所以,上面的示例代碼可以使用 sort.Ints() 函數(shù)排序數(shù)據(jù)。
示例代碼:
- func main () {
- intSlice := IntSlice([]int{9, 7, 5, 3, 1})
- fmt.Println(intSlice) // 排序前
- sort.Ints(intSlice)
- fmt.Println(intSlice) // 使用 sort.Ints() 排序數(shù)據(jù)
- }
除了 sort.Ints(),還有 sort.Float64s(),sort.Strings()等。
03自定義集合排序
在 Golang 語言項(xiàng)目開發(fā)中,我們經(jīng)常會(huì)使用結(jié)構(gòu)體,如果我們需要排序結(jié)構(gòu)體類型的切片,應(yīng)該怎么操作呢?
我們可以按照 Part 01 介紹的方式,實(shí)現(xiàn)那三個(gè)方法,然后調(diào)用 sort.Sort() 函數(shù),當(dāng)然,sort 包也為我們封裝了排序結(jié)構(gòu)體類型切片的函數(shù) sort.Slice(),但是,參數(shù)除了需要排序的數(shù)據(jù)之外,還需要提供一個(gè) Less() 函數(shù)類型的參數(shù)。
示例代碼:
- people := []struct {
- Name string
- Age int
- }{
- {"Gopher", 7},
- {"Alice", 55},
- {"Vera", 24},
- {"Bob", 75},
- }
- sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name })
- fmt.Println("By name:", people)
- sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age })
- fmt.Println("By age:", people)
輸出結(jié)果:
- By name: [{Alice 55} {Bob 75} {Gopher 7} {Vera 24}]
- By age: [{Gopher 7} {Vera 24} {Alice 55} {Bob 75}]
04總結(jié)
本文我們介紹了怎么使用 Golang 語言標(biāo)準(zhǔn)庫 sort 包排序數(shù)據(jù),需要注意的是,除了本文使用的類型之外,其它任意類型只要實(shí)現(xiàn) sort.Interface 的三個(gè)方法,都可以調(diào)用 sort.Sort() 函數(shù)排序數(shù)據(jù)。
另外,除了排序數(shù)據(jù)之外,sort 包也為我們提供了查找功能的函數(shù) sort.Search(),感興趣的讀者朋友們可以閱讀 Golang 語言官方標(biāo)準(zhǔn)庫文檔了解更多。
本文轉(zhuǎn)載自微信公眾號(hào)「Golang語言開發(fā)?!梗梢酝ㄟ^以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系Golang語言開發(fā)棧公眾號(hào)。