24

A stupid question. I can't sort using default sort function in go

package main
import "fmt"
import "sort"

func main() {
    var arr [5]int
    fmt.Println("Enter 5 elements")
    for i:=0;i<5;i++{
        fmt.Scanf("%d",&arr[i])
    }
    sort.Ints(arr)
    fmt.Println(arr)
}

When executing the above program, It throws out

cannot use arr (type [5]int) as type []int in argument to sort.Ints

Need Help.

1
  • 4
    sort.Ints takes a slice, not an array. Commented Aug 30, 2016 at 4:45

1 Answer 1

34

sort.Ints expects a slice of int, not an array. Easiest fix is to change

sort.Ints(arr)

to

sort.Ints(arr[:])
Sign up to request clarification or add additional context in comments.

4 Comments

What change is arr[:] actually making?
What is the complexity of this default sort function?
There is no "default sort function" as far as sorting algorithm used. Go uses different algorithms based on some criteria. You can read through them in the comments or by following along the code here: golang.org/src/sort/sort.go

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.