I have created a multidimensional array (slice) in Go as follows:
var distancematrix [5][5]int
So it is a 5*5 array/slice. Now I am inserting values into this slice such that at a point:
distancematrix : [[0 154 12 35 138] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]]
Now, I want to sort this array in ascending order, e.g.:
sorteddistancematrix : [[0 12 35 138 154] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]]
I tried sort.Ints(distancematrix[0])
but it throws an error saying:
cannot use distancematrix[0] (type [5]int) as type []int in argument to sort.Ints
Basically, I want to fetch the smallest non-zero value in the array. How can I sort this array to achieve this?
To get the smallest non-zero element, you don't need to sort it. Sorting an array or slice is relatively a costly operation - compared to just getting the smallest non-zero element.
Generally to get the smallest non-zero element, just loop over the values, and look for the value that fits you best. If you find a better one (in your example a smaller non-zero), keep that and continue.
Example implementation:
func smallestNonZero(s []int) (n int) {
for _, v := range s {
if v != 0 && (v < n || n == 0) {
n = v
}
}
return
}
Note: This function will return 0
if and only if the passed slice does not contain any non-zero element (that is, it's either full of 0
s or it's empty or it's nil
). This function also works properly if the slice (also) contains negative numbers.
If you have an array and not a slice, simply slice the array (which results in a slice) and so you can pass it to the function above.
Using it:
fmt.Println(smallestNonZero([]int{5, 3, 1, 4}))
fmt.Println(smallestNonZero([]int{0, 3, 5, 8, 0, 2, 9}))
arr := [5]int{0, 154, 12, 35, 138}
fmt.Println(smallestNonZero(arr[:]))
Output (try it on the Go Playground):
1
2
12
The Go Programming Language Specification
Slice expressions construct a substring or slice from a string, array, pointer to array, or slice. There are two variants: a simple form that specifies a low and high bound, and a full form that also specifies a bound on the capacity.
Simple slice expressions
For a string, array, pointer to array, or slice a, the primary expression
a[low : high]
constructs a substring or slice. The indices low and high select which elements of operand a appear in the result. The result has indices starting at 0 and length equal to high - low. After slicing the array a
a := [5]int{1, 2, 3, 4, 5} s := a[1:4]
the slice s has type []int, length 3, capacity 4, and elements
s[0] == 2 s[1] == 3 s[2] == 4
For convenience, any of the indices may be omitted. A missing low index defaults to zero; a missing high index defaults to the length of the sliced operand:
a[2:] // same as a[2 : len(a)] a[:3] // same as a[0 : 3] a[:] // same as a[0 : len(a)]
If a is a pointer to an array, a[low : high] is shorthand for (*a)[low : high].
To convert type [5]int
to type []int
, slice the array. For example,
package main
import "sort"
func main() {
var distancematrix [5][5]int
sort.Ints(distancematrix[0][:])
}
var distancematrix [5][5]int, will create multidimensional array slice of 5 * 5 int. And when you try to access distancematrix[0], it returns int array slice of type [5]int. Where as sort.Ints expects type []int.
Here below I have declared distancematrix of type [][] and hence distancematrix[0] returns slice array of type []int.
package main
import (
"fmt"
"sort"
)
func main() {
distancematrix := [][]int{{0, 154, 12, 35, 138}, {0, 0, 0, 0, 0}}
sort.Ints(distancematrix[0])
fmt.Println(distancematrix[0])
}