经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Go语言 » 查看文章
golang中按照结构体的某个字段排序实例代码
来源:jb51  时间:2022/5/30 9:33:54  对本文有异议

概述

golang的sort包默认支持int, float64, string的从小大到排序:

int -> Ints(x []int)
float64 -> Float64s(x []float64)
string -> Strings(x []string)

同时它还提供了自定义的排序接口Interface,此接口保护三个方法。

  1. type Interface interface {
  2. // Len is the number of elements in the collection.
  3. Len() int
  4. // Less reports whether the element with
  5. // index i should sort before the element with index j.
  6. Less(i, j int) bool
  7. // Swap swaps the elements with indexes i and j.
  8. Swap(i, j int)
  9. }

golang默认提供了三个类型,他们都实现了Interface:
Float64Slice
IntSlice
StringSlice

从大到小排序

方法1:先使用提供的从大到小排序,再翻转

  1. arr := []float64{0.1, 0.5, 0.8, 0.4, 0.2}
  2.  
  3. sort.Sort(sort.Reverse(sort.Float64Slice(arr)))
  4. fmt.Println(arr) // [0.8 0.5 0.4 0.2 0.1]

方法二:自定义类型实现

  1. type Float64SliceDecrement []float64
  2. func (s Float64SliceDecrement) Len() int { return len(s) }
  3. func (s Float64SliceDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  4. func (s Float64SliceDecrement) Less(i, j int) bool { return s[i] > s[j] }
  5. func main() {
  6. arr := []float64{0.1, 0.5, 0.8, 0.4, 0.2}
  7. sort.Sort(Float64SliceDecrement(arr))
  8. fmt.Println(arr) // [0.8 0.5 0.4 0.2 0.1]
  9. }

按照结构体的某个字段排序

按年纪从大到小排序

  1. type Persons struct {
  2. Age int
  3. Height int
  4. }
  5.  
  6. type PersonsSliceDecrement []Persons
  7. func (s PersonsSliceDecrement) Len() int { return len(s) }
  8. func (s PersonsSliceDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  9. func (s PersonsSliceDecrement) Less(i, j int) bool { return s[i].Age > s[j].Age }
  10. func main() {
  11. arr1 := []Persons{
  12. Persons{10, 12},
  13. Persons{20, 12},
  14. Persons{9, 12},
  15. Persons{10, 12},
  16. Persons{11, 12},
  17. }
  18. sort.Sort(PersonsSliceDecrement(arr1))
  19. fmt.Println(arr1)
  20. }
  21.  

打印

[{20 12} {11 12} {10 12} {10 12} {9 12}]

按年纪从大到小,如果年纪相等的,按身高从小到到

  1. type Persons struct {
  2. Age int
  3. Height int
  4. }
  5.  
  6. type PersonsSliceDecrement []Persons
  7. func (s PersonsSliceDecrement) Len() int { return len(s) }
  8. func (s PersonsSliceDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  9. func (s PersonsSliceDecrement) Less(i, j int) bool {
  10. if s[i].Age > s[j].Age {
  11. return true
  12. }
  13. if s[i].Age == s[j].Age && s[i].Height < s[j].Height {
  14. return true
  15. }
  16. return false
  17. }
  18.  
  19. func main() {
  20. arr1 := []Persons{
  21. Persons{10, 120},
  22. Persons{20, 12},
  23. Persons{10, 110},
  24. Persons{10, 11},
  25. Persons{10, 100},
  26. }
  27. sort.Sort(PersonsSliceDecrement(arr1))
  28. fmt.Println(arr1)
  29. }

打印

[{20 12} {10 11} {10 100} {10 110} {10 120}]

使用 sort.Stable 进行稳定排序

sort.Sort 并不保证排序的稳定性。如果有需要, 可以使用 sort.Stable ,用法就是将sort.Sort 替换为 sort.Stable

附:go根据结构体中任意字段进行排序

附:根据结构体中任意字段进行排序

Sort()

Reverse()

Less(i,j int) bool

Len() int

Swap(i,j int)

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "sort"
  6. )
  7.  
  8. type Student struct {
  9. Number string
  10. Name string
  11. Age int
  12. IsWalker bool
  13. Weight float32
  14. }
  15.  
  16. type ByNumber []*Student
  17.  
  18. func (this ByNumber)Len() int {
  19. return len(this)
  20. }
  21.  
  22. func (this ByNumber)Less(i,j int) bool {
  23. return this[i].Number<this[j].Number
  24. }
  25.  
  26. func (this ByNumber)Swap(i,j int) {
  27. this[i],this[j] = this[j],this[i]
  28. }
  29.  
  30. func (this ByNumber) String() string {
  31. const format = "| %v |\t%v |\t%v |\t %v |\t %v |\t%v |\n"
  32. fmt.Println("\t\t\t\t\t学生信息表")
  33. fmt.Println(" 序号\t学号 \t姓名\t 年龄\t 体重\t 是否走读")
  34. for k,v:=range this{
  35. fmt.Printf(format,k+1,v.Number,v.Name,v.Age,v.Weight,v.IsWalker)
  36. }
  37. return ""
  38. }
  39.  
  40. func main1() {
  41. sts:=[]*Student{
  42. &Student{Number: "003",Name: "张三"},
  43. &Student{Number: "004",Name: "张四"},
  44. &Student{Number: "001",Name: "张一"},
  45. &Student{Number: "002",Name: "张二"},
  46. &Student{Number: "000",Name: "张零"},
  47. }
  48. b:=ByNumber(sts)
  49. sort.Sort(b)
  50. fmt.Println(b)
  51. fmt.Println("反转")
  52. sort.Sort(sort.Reverse(b)) //反转的用法
  53. fmt.Println(b)
  54.  
  55.  
  56. //为结构体内的每一个字段都绑定一个排序的外壳,这种操作显然不是很聪明
  57. //这时候使用组合来解决这个问题
  58. }
  59.  
  60.  
  61. type customSort struct {
  62. s []*Student
  63. less func(i,j *Student) bool
  64. }
  65.  
  66. func (this *customSort)Len() int {
  67. return len(this.s)
  68. }
  69.  
  70. func (this *customSort)Swap(i,j int) {
  71. this.s[i],this.s[j] = this.s[j],this.s[i]
  72. }
  73.  
  74. func (this *customSort)Less(i,j int) bool {
  75. return this.less(this.s[i],this.s[j])
  76. }
  77.  
  78. func main() {
  79. sts:=[]*Student{
  80. &Student{Number: "003",Name: "张三"},
  81. &Student{Number: "004",Name: "张四"},
  82. &Student{Number: "001",Name: "张一"},
  83. &Student{Number: "000",Name: "张二"},
  84. &Student{Number: "002",Name: "张二"},
  85. }
  86.  
  87. c:=&customSort{
  88. s: sts,
  89. less: func(i, j *Student) bool {
  90. if i.Number != j.Number { //可以指定多种排序规则
  91. return i.Number>j.Number
  92. }
  93. if i.Name!=j.Name{
  94. return i.Name<j.Name
  95. }
  96. return false
  97. },
  98. }
  99.  
  100. /*
  101. package sort
  102. // A type, typically a collection, that satisfies sort.Interface can be
  103. // sorted by the routines in this package. The methods require that the
  104. // elements of the collection be enumerated by an integer index.
  105. type Interface interface {
  106. // Len is the number of elements in the collection.
  107. Len() int
  108. // Less reports whether the element with
  109. // index i should sort before the element with index j.
  110. Less(i, j int) bool
  111. // Swap swaps the elements with indexes i and j.
  112. Swap(i, j int)
  113. }
  114. */
  115. sort.Sort(c) //Sort方法中不只能放slice类型,还可以放结构体类型,只要改类型 实现 Sort接口
  116. fmt.Println(ByNumber(sts)) //单纯的使用一下ByNumber中重写是String()方法
  117. }

总结

到此这篇关于golang中按照结构体的某个字段排序的文章就介绍到这了,更多相关golang按字段排序内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号