du4822
2018-09-21 21:44
浏览 267
已采纳

如何在golang中对定长数组进行排序?

I have the following multivariate array:

x := [2][3]int{
    {3, 2, 1},
    {3, 2, 1},
}

Both rows and columns are fixed-size.

I'm trying to check that the rows are sorted, and I undertand the sort function requires arrays without a known size. How can I ask the go to treat the item with a fixed-, known size as if it had an unknown size?

var allTrue bool = true
for i := range x {
  t := sort.Ints(x[i]) == []int{1, 2, 3}
  allTrue = allTrue && t
}

I get:

./main.go:xx:yy: sort.Ints(x[i]) used as value
./main.go:xx:yy: cannot use x[i] (type [3]int) as type []int in argument to sort.Ints

Am I reading this error message correctly?

3条回答 默认 最新

相关推荐 更多相似问题