Go lang sort a 2D Array

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

Go lang sort a 2D Array

问题

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

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

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

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

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,

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

Then is there something like,

sort.Sort(matrix)

答案1

得分: 16

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

type Matrix [3][3]int

func (m Matrix) Len() int { return len(m) }
func (m Matrix) Less(i, j int) bool {
    for x := range m[i] {
        if m[i][x] == m[j][x] {
            continue
        }
        return m[i][x] < m[j][x]
    }
    return false
}

func (m *Matrix) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func main() {
    m := Matrix(matrix)
    sort.Sort(&m)
}

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

sort.Slice(matrix[:], func(i, j int) bool {
    for x := range matrix[i] {
        if matrix[i][x] == matrix[j][x] {
            continue
        }
        return matrix[i][x] < matrix[j][x]
    }
    return false
})

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

type Matrix [3][3]int

func (m Matrix) Len() int { return len(m) }
func (m Matrix) Less(i, j int) bool {
	for x := range m[i] {
		if m[i][x] == m[j][x] {
			continue
		}
		return m[i][x] &lt; m[j][x]
	}
	return false
}

func (m *Matrix) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func main() {
	m := Matrix(matrix)
	sort.Sort(&amp;m)
}

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

sort.Slice(matrix[:], func(i, j int) bool {
	for x := range matrix[i] {
		if matrix[i][x] == matrix[j][x] {
			continue
		}
		return matrix[i][x] &lt; matrix[j][x]
	}
	return false
})

fmt.Println(matrix)

答案2

得分: 10

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

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

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

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

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

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

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

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

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:

确定