golang对自定义类型进行排序的解决方法

golang对自定义类型进行排序的解决方法,第1张

概述前言Go语言支持我们自定义类型,我们大家在实际项目中,常常需要根据一个结构体类型的某个字段进行排序。之前遇到这个问题不知道如何解决,后来在网上搜索了相关问题,找到了一些好的解决方案,此处参考下,做个总结

前言

Go 语言支持我们自定义类型,我们大家在实际项目中,常常需要根据一个结构体类型的某个字段进行排序。之前遇到这个问题不知道如何解决,后来在网上搜索了相关问题,找到了一些好的解决方案,此处参考下,做个总结吧。

由于 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,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,b int)// 快速排序func quickSort(data Interface,b,maxDepth int)// 归并排序func symMerge(data Interface,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[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.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,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",{ name: "test1",{ name: "test12",person.Age) } sort.sort(SortByname{persons}) fmt.Println("排序后") for _,person.Age) }}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对编程小技巧的支持。

总结

以上是内存溢出为你收集整理的golang对自定义类型进行排序的解决方法全部内容,希望文章能够帮你解决golang对自定义类型进行排序的解决方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1294249.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存