Sorting a slice

Is there anyway to sort a slice and assign it to another variable? I noticed that the method sort.Ints() mutates the original slice.

func main() {
	xi := []int{4, 7, 3, 42, 99, 18, 16, 56, 12}
	fmt.Println(xi)
	sort.Ints(xi)
	fmt.Println(xi)


}

I tried using these:
newXi := sort.Ints(xi)

var newXi1 int[]
newXi1 = sort.Ints(xi)
var newXi2 int[]
append(newXi2, sort.Ints(xi))

but none of them work. Any tips?

Copy the slice and then sort the copy

LMAO why did i not think of that… I am ASHAMED. Thanks man!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.