如何按多个值对Go切片进行排序?

huangapple go评论177阅读模式
英文:

How do I sort a go slice by multiple values?

问题

我有一个类似上面的结构体切片。如何按照X值首先排序,然后按照Y值排序,就像在SQL中使用ORDER BY X,Y一样?

我看到你可以使用sort.Slice()自Go 1.8版本以来,但是否有一种简单的方法可以在不多次循环切片的情况下解决这个问题?

英文:
  1. type Item struct {
  2. Y int
  3. X int
  4. otherProp int
  5. }

I have a slice of structs like the above one. How do I sort the slice item []Item by the X value first and then by the Y value like in SQLs ORDER BY X,Y?

I see that you can use sort.Slice() since go 1.8 but is there a simple way to solve this without looping over the slice several times?

答案1

得分: 2

根据这里的第一个示例:Package sort,我编写了以下代码...

Less()函数中,我检查X是否相等,如果相等,则检查Y

playground演示

  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. type Item struct {
  7. X int
  8. Y int
  9. otherProp int
  10. }
  11. func (i Item) String() string {
  12. return fmt.Sprintf("X: %d, Y: %d, otherProp: %d\n", i.X, i.Y, i.otherProp)
  13. }
  14. // ByX 根据 X 字段为 []Item 实现 sort.Interface 接口。
  15. type ByX []Item
  16. func (o ByX) Len() int { return len(o) }
  17. func (o ByX) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
  18. func (o ByX) Less(i, j int) bool {
  19. if o[i].X == o[j].X {
  20. return o[i].Y < o[j].Y
  21. } else {
  22. return o[i].X < o[j].X
  23. }
  24. }
  25. func main() {
  26. items := []Item{
  27. {1,2,3},
  28. {5,2,3},
  29. {3,2,3},
  30. {9,2,3},
  31. {1,1,3},
  32. {1,0,3},
  33. }
  34. fmt.Println(items)
  35. sort.Sort(ByX(items))
  36. fmt.Println(items)
  37. }

输出:

  1. [X: 1, Y: 2, otherProp: 3
  2. X: 5, Y: 2, otherProp: 3
  3. X: 3, Y: 2, otherProp: 3
  4. X: 9, Y: 2, otherProp: 3
  5. X: 1, Y: 1, otherProp: 3
  6. X: 1, Y: 0, otherProp: 3
  7. ]
  8. [X: 1, Y: 0, otherProp: 3
  9. X: 1, Y: 1, otherProp: 3
  10. X: 1, Y: 2, otherProp: 3
  11. X: 3, Y: 2, otherProp: 3
  12. X: 5, Y: 2, otherProp: 3
  13. X: 9, Y: 2, otherProp: 3
  14. ]
英文:

Following the first example from here: Package sort, I wrote the following...

Inside the Less() function I check if X are equal and if so, I then check Y.

playground demo

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sort&quot;
  5. )
  6. type Item struct {
  7. X int
  8. Y int
  9. otherProp int
  10. }
  11. func (i Item) String() string {
  12. return fmt.Sprintf(&quot;X: %d, Y: %d, otherProp: %d\n&quot;, i.X, i.Y, i.otherProp)
  13. }
  14. // ByX implements sort.Interface for []Item based on
  15. // the X field.
  16. type ByX []Item
  17. func (o ByX) Len() int { return len(o) }
  18. func (o ByX) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
  19. func (o ByX) Less(i, j int) bool {
  20. if o[i].X == o[j].X {
  21. return o[i].Y &lt; o[j].Y
  22. } else {
  23. return o[i].X &lt; o[j].X
  24. }
  25. }
  26. func main() {
  27. items := []Item{
  28. {1,2,3},
  29. {5,2,3},
  30. {3,2,3},
  31. {9,2,3},
  32. {1,1,3},
  33. {1,0,3},
  34. }
  35. fmt.Println(items)
  36. sort.Sort(ByX(items))
  37. fmt.Println(items)
  38. }

output:

  1. [X: 1, Y: 2, otherProp: 3
  2. X: 5, Y: 2, otherProp: 3
  3. X: 3, Y: 2, otherProp: 3
  4. X: 9, Y: 2, otherProp: 3
  5. X: 1, Y: 1, otherProp: 3
  6. X: 1, Y: 0, otherProp: 3
  7. ]
  8. [X: 1, Y: 0, otherProp: 3
  9. X: 1, Y: 1, otherProp: 3
  10. X: 1, Y: 2, otherProp: 3
  11. X: 3, Y: 2, otherProp: 3
  12. X: 5, Y: 2, otherProp: 3
  13. X: 9, Y: 2, otherProp: 3
  14. ]

答案2

得分: 1

有没有一种简单的方法可以在不多次循环切片的情况下解决这个问题?

没有。基于比较的排序基本上总是需要至少一次循环切片加上一些额外的操作。但不用担心:sort.Slice并不会做太多的工作。

你有什么问题吗?

英文:

> [...] is there a simple way to solve this without looping over the slice several times?

No. Comparison-based sorting basically always involves looping over the slice at least once plus a bit more. But do not worry: sort.Slice does not do too much work.

What's your question?

huangapple
  • 本文由 发表于 2017年9月13日 21:50:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/46199397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定