3

I'm attempting to sort an array of Ints using the following function

func sortArray(array [100]int) [100]int {
    var sortedArray = array
    sort.Sort(sort.Ints(sortedArray))

    return sortedArray
}

and getting the following error:

.\helloworld.go:22: cannot use sortedArray (type [100]int) as type []int in argument to sort.Ints .\helloworld.go:22: sort.Ints(sortedArray) used as value

I'm trying to figure out Go and I'm getting stuck on this one.

1
  • 1
    sort.Ints (and indeed sort.Sort) expect a slice and you gave it an array. Commented Feb 8, 2017 at 21:05

2 Answers 2

8

You can sort an array by taking a slice of the entire array

sort.Ints(array[:])

You probably don't want an array in the first place however, and should be using a slice of []int.

Also, your sortedArray is the same value as array, so there is no reason to create the second variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Cheers, it worked. I'll read some more on slices to figure out the why. Thanks. I fixed my redundancy as well. Why does the second error message occur?
@Kaylined: The second error message is because you're trying to pass sort.Ints as an argument to sort.Sort. sort.Ints doesn't have a return value, and you don't need to call sort.Sort.
0

You probably don't want an array in the first place however, and should be using a slice of []int.

Also, your sortedArray is the same value as array, so there is no reason to create the second variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.