Junior — Middle
How will the structure of the slice change after calling sort.Slice in Go?
sobes.tech AI
Answer from AI
After calling sort.Slice in Go, the structure of the slice does not change in terms of its internal setup: the slice still contains a pointer to the array, along with its length and capacity. However, the elements that the slice points to will be sorted according to the specified comparison function. That is, the order of elements in the underlying array will change, but the slice itself will remain the same in structure and size.
Example:
nums := []int{3, 1, 4, 2}
sort.Slice(nums, func(i, j int) bool {
return nums[i] < nums[j]
})
// nums now: [1, 2, 3, 4]
Thus, sort.Slice sorts the elements in place, without creating a new slice or changing its length or capacity.