您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“golang中如何按照結(jié)構(gòu)體的某個(gè)字段排序”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“golang中如何按照結(jié)構(gòu)體的某個(gè)字段排序”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
golang的sort包默認(rèn)支持int, float64, string的從小大到排序:
int -> Ints(x []int)
float64 -> Float64s(x []float64)
string -> Strings(x []string)
同時(shí)它還提供了自定義的排序接口Interface,此接口保護(hù)三個(gè)方法。
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) }
golang默認(rèn)提供了三個(gè)類(lèi)型,他們都實(shí)現(xiàn)了Interface:
Float64Slice
IntSlice
StringSlice
方法1:先使用提供的從大到小排序,再翻轉(zhuǎn)
arr := []float64{0.1, 0.5, 0.8, 0.4, 0.2} sort.Sort(sort.Reverse(sort.Float64Slice(arr))) fmt.Println(arr) // [0.8 0.5 0.4 0.2 0.1]
方法二:自定義類(lèi)型實(shí)現(xiàn)
type Float64SliceDecrement []float64 func (s Float64SliceDecrement) Len() int { return len(s) } func (s Float64SliceDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s Float64SliceDecrement) Less(i, j int) bool { return s[i] > s[j] } func main() { arr := []float64{0.1, 0.5, 0.8, 0.4, 0.2} sort.Sort(Float64SliceDecrement(arr)) fmt.Println(arr) // [0.8 0.5 0.4 0.2 0.1] }
按年紀(jì)從大到小排序
type Persons struct { Age int Height int } type PersonsSliceDecrement []Persons func (s PersonsSliceDecrement) Len() int { return len(s) } func (s PersonsSliceDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s PersonsSliceDecrement) Less(i, j int) bool { return s[i].Age > s[j].Age } func main() { arr1 := []Persons{ Persons{10, 12}, Persons{20, 12}, Persons{9, 12}, Persons{10, 12}, Persons{11, 12}, } sort.Sort(PersonsSliceDecrement(arr1)) fmt.Println(arr1) }
打印
[{20 12} {11 12} {10 12} {10 12} {9 12}]
按年紀(jì)從大到小,如果年紀(jì)相等的,按身高從小到到
type Persons struct { Age int Height int } type PersonsSliceDecrement []Persons func (s PersonsSliceDecrement) Len() int { return len(s) } func (s PersonsSliceDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s PersonsSliceDecrement) Less(i, j int) bool { if s[i].Age > s[j].Age { return true } if s[i].Age == s[j].Age && s[i].Height < s[j].Height { return true } return false } func main() { arr1 := []Persons{ Persons{10, 120}, Persons{20, 12}, Persons{10, 110}, Persons{10, 11}, Persons{10, 100}, } sort.Sort(PersonsSliceDecrement(arr1)) fmt.Println(arr1) }
打印
[{20 12} {10 11} {10 100} {10 110} {10 120}]
sort.Sort 并不保證排序的穩(wěn)定性。如果有需要, 可以使用 sort.Stable ,用法就是將sort.Sort 替換為 sort.Stable
附:go根據(jù)結(jié)構(gòu)體中任意字段進(jìn)行排序
Sort()
Reverse()
Less(i,j int) bool
Len() int
Swap(i,j int)
package main import ( "fmt" "sort" ) type Student struct { Number string Name string Age int IsWalker bool Weight float32 } type ByNumber []*Student func (this ByNumber)Len() int { return len(this) } func (this ByNumber)Less(i,j int) bool { return this[i].Number<this[j].Number } func (this ByNumber)Swap(i,j int) { this[i],this[j] = this[j],this[i] } func (this ByNumber) String() string { const format = "| %v |\t%v |\t%v |\t %v |\t %v |\t%v |\n" fmt.Println("\t\t\t\t\t學(xué)生信息表") fmt.Println(" 序號(hào)\t學(xué)號(hào) \t姓名\t 年齡\t 體重\t 是否走讀") for k,v:=range this{ fmt.Printf(format,k+1,v.Number,v.Name,v.Age,v.Weight,v.IsWalker) } return "" } func main1() { sts:=[]*Student{ &Student{Number: "003",Name: "張三"}, &Student{Number: "004",Name: "張四"}, &Student{Number: "001",Name: "張一"}, &Student{Number: "002",Name: "張二"}, &Student{Number: "000",Name: "張零"}, } b:=ByNumber(sts) sort.Sort(b) fmt.Println(b) fmt.Println("反轉(zhuǎn)") sort.Sort(sort.Reverse(b)) //反轉(zhuǎn)的用法 fmt.Println(b) //為結(jié)構(gòu)體內(nèi)的每一個(gè)字段都綁定一個(gè)排序的外殼,這種操作顯然不是很聰明 //這時(shí)候使用組合來(lái)解決這個(gè)問(wèn)題 } type customSort struct { s []*Student less func(i,j *Student) bool } func (this *customSort)Len() int { return len(this.s) } func (this *customSort)Swap(i,j int) { this.s[i],this.s[j] = this.s[j],this.s[i] } func (this *customSort)Less(i,j int) bool { return this.less(this.s[i],this.s[j]) } func main() { sts:=[]*Student{ &Student{Number: "003",Name: "張三"}, &Student{Number: "004",Name: "張四"}, &Student{Number: "001",Name: "張一"}, &Student{Number: "000",Name: "張二"}, &Student{Number: "002",Name: "張二"}, } c:=&customSort{ s: sts, less: func(i, j *Student) bool { if i.Number != j.Number { //可以指定多種排序規(guī)則 return i.Number>j.Number } if i.Name!=j.Name{ return i.Name<j.Name } return false }, } /* package sort // 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) } */ sort.Sort(c) //Sort方法中不只能放slice類(lèi)型,還可以放結(jié)構(gòu)體類(lèi)型,只要改類(lèi)型 實(shí)現(xiàn) Sort接口 fmt.Println(ByNumber(sts)) //單純的使用一下ByNumber中重寫(xiě)是String()方法 }
讀到這里,這篇“golang中如何按照結(jié)構(gòu)體的某個(gè)字段排序”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。