Go lang sort a 2D Array

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

Go lang sort a 2D Array

问题

我想在Go语言中对一个二维数组进行排序。有人可以建议我如何操作吗?

例如,如果我有以下数组:

  1. var matrix [3][3]int{
  2. {2,3,1},
  3. {6,3,5},
  4. {1,4,9}
  5. }

那么是否有类似以下的方法:

  1. sort.Sort(matrix)
英文:

I want to sort a two dimensional array in Go. Can anyone please suggest how I can go about this?

For example If I have,

  1. var matrix [3][3]int{
  2. {2,3,1},
  3. {6,3,5},
  4. {1,4,9}
  5. }

Then is there something like,

  1. sort.Sort(matrix)

答案1

得分: 16

你需要自己定义如何对这种类型进行排序。你可以创建必要的方法来使用sort.Sort接口,必要时使用指针来改变数组的值:https://play.golang.org/p/thdf-k2k3o

  1. type Matrix [3][3]int
  2. func (m Matrix) Len() int { return len(m) }
  3. func (m Matrix) Less(i, j int) bool {
  4. for x := range m[i] {
  5. if m[i][x] == m[j][x] {
  6. continue
  7. }
  8. return m[i][x] < m[j][x]
  9. }
  10. return false
  11. }
  12. func (m *Matrix) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
  13. func main() {
  14. m := Matrix(matrix)
  15. sort.Sort(&m)
  16. }

或者使用sort.Slice函数,将matrix转换为切片,并提供一个适当的比较函数:https://play.golang.org/p/4hrghm9gib

  1. sort.Slice(matrix[:], func(i, j int) bool {
  2. for x := range matrix[i] {
  3. if matrix[i][x] == matrix[j][x] {
  4. continue
  5. }
  6. return matrix[i][x] < matrix[j][x]
  7. }
  8. return false
  9. })
  10. fmt.Println(matrix)
英文:

You have to define how to sort this type yourself. You can either create the necessary methods to use the sort.Sort interface, using a pointer as necessary to mutate the array values: https://play.golang.org/p/thdf-k2k3o

  1. type Matrix [3][3]int
  2. func (m Matrix) Len() int { return len(m) }
  3. func (m Matrix) Less(i, j int) bool {
  4. for x := range m[i] {
  5. if m[i][x] == m[j][x] {
  6. continue
  7. }
  8. return m[i][x] &lt; m[j][x]
  9. }
  10. return false
  11. }
  12. func (m *Matrix) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
  13. func main() {
  14. m := Matrix(matrix)
  15. sort.Sort(&amp;m)
  16. }

Or use the sort.Slice function, converting matrix to a slice and providing an appropriate less function: https://play.golang.org/p/4hrghm9gib

  1. sort.Slice(matrix[:], func(i, j int) bool {
  2. for x := range matrix[i] {
  3. if matrix[i][x] == matrix[j][x] {
  4. continue
  5. }
  6. return matrix[i][x] &lt; matrix[j][x]
  7. }
  8. return false
  9. })
  10. fmt.Println(matrix)

答案2

得分: 10

考虑到您有一个m*n的矩阵,您想要根据第k列进行排序。

  1. sort.SliceStable(rangearray, func(i, j int) bool {
  2. return rangearray[i][k] < rangearray[j][k]
  3. })

同样地,您想要对m*n矩阵进行排序,并且希望根据第k行进行排序。

  1. sort.SliceStable(rangearray, func(i, j int) bool {
  2. return rangearray[k][i] < rangearray[k][j]
  3. })
英文:

Consider You have a m*n matrix and you want to sort based on kth column

  1. sort.SliceStable(rangearray, func(i, j int) bool {
  2. return rangearray[i][k] &lt; rangearray[j][k]
  3. })

In the same way you want to sort the m*n matrix and you want to sort based on kth row

  1. sort.SliceStable(rangearray, func(i, j int) bool {
  2. return rangearray[k][i] &lt; rangearray[k][j]
  3. })

huangapple
  • 本文由 发表于 2017年3月6日 23:32:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/42629541.html
匿名

发表评论

匿名网友

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

确定