经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Go语言 » 查看文章
golang编程开发使用sort排序示例详解
来源:jb51  时间:2021/11/15 20:02:33  对本文有异议

golang sort package: https://studygolang.com/articles/3360

sort 操作的对象通常是一个 slice,需要满足三个基本的接口,并且能够使用整数来索引

  1. // A type, typically a collection, that satisfies sort.Interface can be
  2. // sorted by the routines in this package. The methods require that the
  3. // elements of the collection be enumerated by an integer index.
  4. type Interface interface {
  5. // Len is the number of elements in the collection.
  6. Len() int
  7. // Less reports whether the element with
  8. // index i should sort before the element with index j.
  9. Less(i, j int) bool
  10. // Swap swaps the elements with indexes i and j.
  11. Swap(i, j int)
  12. }

ex-1 对 []int 从小到大排序 

  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. type IntSlice []int
  7. func (s IntSlice) Len() int { return len(s) }
  8. func (s IntSlice) Swap(i, j int){ s[i], s[j] = s[j], s[i] }
  9. func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] }
  10. func main() {
  11. a := []int{4,3,2,1,5,9,8,7,6}
  12. sort.Sort(IntSlice(a))
  13. fmt.Println("After sorted: ", a)
  14. }

 ex-2 使用 sort.Ints 和 sort.Strings
golang 对常见的 []int []string 分别定义了 IntSlice StringSlice, 实现了各自的排序接口。而 sort.Ints 和 sort.Strings 可以直接对 []int 和 []string 进行排序, 使用起来非常方便 

  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. func main() {
  7. a := []int{3, 5, 4, -1, 9, 11, -14}
  8. sort.Ints(a)
  9. fmt.Println(a)
  10. ss := []string{"surface", "ipad", "mac pro", "mac air", "think pad", "idea pad"}
  11. sort.Strings(ss)
  12. fmt.Println(ss)
  13. sort.Sort(sort.Reverse(sort.StringSlice(ss)))
  14. fmt.Printf("After reverse: %v\n", ss)
  15. }

ex-3 使用 sort.Reverse 进行逆序排序
如果我们想对一个 sortable object 进行逆序排序,可以自定义一个type。但 sort.Reverse 帮你省掉了这些代码

  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. func main() {
  7. a := []int{4,3,2,1,5,9,8,7,6}
  8. sort.Sort(sort.Reverse(sort.IntSlice(a)))
  9. fmt.Println("After reversed: ", a)
  10. }

ex-4 使用 sort.Stable 进行稳定排序
sort.Sort 并不保证排序的稳定性。如果有需要, 可以使用 sort.Stable 

  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. type person struct {
  7. Name string
  8. Age int
  9. }
  10. type personSlice []person
  11. func (s personSlice) Len() int { return len(s) }
  12. func (s personSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  13. func (s personSlice) Less(i, j int) bool { return s[i].Age < s[j].Age }
  14. func main() {
  15. a := personSlice {
  16. {
  17. Name: "AAA",
  18. Age: 55,
  19. },
  20. {
  21. Name: "BBB",
  22. Age: 22,
  23. },
  24. {
  25. Name: "CCC",
  26. Age: 0,
  27. },
  28. {
  29. Name: "DDD",
  30. Age: 22,
  31. },
  32. {
  33. Name: "EEE",
  34. Age: 11,
  35. },
  36. }
  37. sort.Stable(a)
  38. fmt.Println(a)
  39. }

以上就是go语言编程使用sort来排序示例详解的详细内容,更多关于go语言sort排序的资料请关注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号