前言
Go 語言支持我們自定義類型,我們大家在實際項目中,常常需要根據一個結構體類型的某個字段進行排序。之前遇到這個問題不知道如何解決,后來在網上搜索了相關問題,找到了一些好的解決方案,此處參考下,做個總結吧。
由于 golang/177808.html">golang/196398.html">golang 的 sort 包本身就提供了相應的功能, 我們就沒必要重復的造個輪子了,來看看如何利用 sort 包來實現吧。
sort包淺談
golang中也實現了排序算法的包sort包,sort 包 在內部實現了四種基本的排序算法:插入排序(insertionSort)、歸并排序(symMerge)、堆排序(heapSort)和快速排序(quickSort); sort 包會依據實際數據自動選擇最優的排序算法。
所以我們寫代碼時只需要考慮實現 sort.Interface
這個類型就可以了。
粗略的看看sort包
func Sort(data Interface) { // Switch to heapsort if depth of 2*ceil(lg(n+1)) is reached. n := data.Len() maxDepth := 0 for i := n; i > 0; i >>= 1 { maxDepth++ } maxDepth *= 2 quickSort(data, 0, n, maxDepth)}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)}// 內部實現的四種排序算法// 插入排序func insertionSort(data Interface, a, b int)// 堆排序func heapSort(data Interface, a, b int)// 快速排序func quickSort(data Interface, a, b, maxDepth int)// 歸并排序func symMerge(data Interface, a, m, b int)
所以要調用sort.Sort()
來實現自定義類型排序,只需要我們的類型實現 Interface 接口類型中的三個方法即可。
先看看 sort 包本身對于 []int
類型如何排序
// 首先定義了一個[]int類型的別名IntSlice type IntSlice []int// 獲取此 slice 的長度func (p IntSlice) Len() int { return len(p) }// 比較兩個元素大小 升序func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }// 交換數據func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }// sort.Ints()內部調用Sort() 方法實現排序// 注意 要先將[]int 轉換為 IntSlice類型 因為此類型才實現了Interface的三個方法 func Ints(a []int) { Sort(IntSlice(a)) }
照葫蘆畫瓢 我們來對自定義的結構體類型進行降序排序
package mainimport ( "fmt" "sort")type Person struct { Name string Age int}type Persons []Person// 獲取此 slice 的長度func (p Persons) Len() int { return len(p) }// 根據元素的年齡降序排序 (此處按照自己的業務邏輯寫) func (p Persons) Less(i, j int) bool { return p[i].Age > p[j].Age}// 交換數據func (p Persons) Swap(i, j int) { p[i], p[j] = p[j], p[i] }func main() { persons := Persons{ { Name: "test1", Age: 20, }, { Name: "test2", Age: 22, }, { Name: "test3", Age: 21, }, } fmt.Println("排序前") for _, person := range persons { fmt.Println(person.Name, ":", person.Age) } sort.Sort(persons) fmt.Println("排序后") for _, person := range persons { fmt.Println(person.Name, ":", person.Age) }}
其實,一般 Len()
和 Swap()
基本不做改變,只有涉及到元素比較的 Less()
方法會有所改變。
當我們對某一個結構體中多個字段進行排序時怎么辦,難道每排序一個就寫下這三個方法么,當然不是。我們可以利用嵌套結構體來解決這個問題。因為嵌套結構體可以繼承父結構體的所有屬性和方法
比如我想對上面 Person 的 Name 字段和 Age 對要排序,我們可以利用嵌套結構體來改進一下。
package mainimport ( "fmt" "sort")type Person struct { Name string Age int}type Persons []Person// Len()方法和Swap()方法不用變化// 獲取此 slice 的長度func (p Persons) Len() int { return len(p) }// 交換數據func (p Persons) Swap(i, j int) { p[i], p[j] = p[j], p[i] }// 嵌套結構體 將繼承 Person 的所有屬性和方法// 所以相當于SortByName 也實現了 Len() 和 Swap() 方法type SortByName struct{ Persons }// 根據元素的姓名長度降序排序 (此處按照自己的業務邏輯寫)func (p SortByName) Less(i, j int) bool { return len(p.Persons[i].Name) > len(p.Persons[j].Name)}type SortByAge struct{ Persons }// 根據元素的年齡降序排序 (此處按照自己的業務邏輯寫)func (p SortByAge) Less(i, j int) bool { return p.Persons[i].Age > p.Persons[j].Age}func main() { persons := Persons{ { Name: "test123", Age: 20, }, { Name: "test1", Age: 22, }, { Name: "test12", Age: 21, }, } fmt.Println("排序前") for _, person := range persons { fmt.Println(person.Name, ":", person.Age) } sort.Sort(SortByName{persons}) fmt.Println("排序后") for _, person := range persons { fmt.Println(person.Name, ":", person.Age) }}
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。
新聞熱點
疑難解答