Sorting (sort package)
Go's sort package and the newer slices.Sort family let you order any slice in place — from one-line helpers like sort.Ints to custom comparisons over struct fields.
Learn Sorting (sort package) in our free Go course — an interactive lesson with runnable examples, a practice exercise and a quick reference.
Part of the free Go course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ The Simple Helpers
For the common cases, the sort package has ready-made functions: sort.Ints and sort.Strings sort a slice in place , ascending. Once a slice is sorted, sort.Search (and wrappers like sort.SearchInts ) can binary search it in O(log n), returning the index of the first element that satisfies your test.
2️⃣ Custom Order with sort.Slice
To sort anything else, sort.Slice takes your slice plus a less func func(i, j int) bool that returns true when element i should come before element j . Use sort.SliceStable when equal elements must keep their original relative order — handy for multi-pass sorting where a previous ordering should survive within ties.
3️⃣ slices.Sort & slices.SortFunc (Go 1.21+)
The generic slices package is the modern way. slices.Sort orders any slice of an ordered type, and slices.SortFunc takes a compare func returning a negative, zero, or positive number — cmp.Compare makes writing it trivial. Swap the two arguments to cmp.Compare to reverse the order.
🎯 Your Turn
Sort the scores from highest to lowest by wrapping an IntSlice in sort.Reverse . Fill in the blank.
Sort the books by Year ascending, breaking ties alphabetically by Title . In the less func, compare Year first and fall through to Title only when the years are equal.
Practice quiz
What does sort.Ints(xs) return?
- A new sorted slice
- The smallest element
- Nothing — it sorts in place
- An error
Answer: Nothing — it sorts in place. sort.Ints sorts the slice in place, ascending, and returns nothing.
What must the less func passed to sort.Slice return?
- true when element i should come before element j
- -1, 0, or 1
- the larger of i and j
- the swapped slice
Answer: true when element i should come before element j. less(i, j) returns true when element i should sort before element j.
When should you use sort.SliceStable instead of sort.Slice?
- When the slice is large
- When sorting strings
- Never — they are identical
- When equal elements must keep their original relative order
Answer: When equal elements must keep their original relative order. Stable sorting preserves the order of elements that compare equal.
How do you sort a []int in descending order using the sort package helpers?
- sort.Ints then reverse the slice manually only
- sort.Sort(sort.Reverse(sort.IntSlice(xs)))
- sort.IntsDesc(xs)
- sort.Reverse(xs)
Answer: sort.Sort(sort.Reverse(sort.IntSlice(xs))). Wrap sort.IntSlice in sort.Reverse: sort.Sort(sort.Reverse(sort.IntSlice(xs))).
What does slices.SortFunc's compare func return?
- A negative, zero, or positive int
- A bool
- The sorted slice
- An index
Answer: A negative, zero, or positive int. SortFunc takes a compare returning <0, 0, or >0; cmp.Compare produces that.
How do you reverse the order with cmp.Compare in slices.SortFunc?
- Negate the slice
- Return -1 always
- Swap the arguments: cmp.Compare(b, a)
- Use slices.Reverse only
Answer: Swap the arguments: cmp.Compare(b, a). Swapping the arguments to cmp.Compare(b, a) reverses the ordering.
What does sort.SearchInts require of the slice and return?
- Unsorted slice; returns the max
- A sorted slice; returns the index of the first element >= target
- Any slice; returns true/false
- A sorted slice; returns the target value
Answer: A sorted slice; returns the index of the first element >= target. It binary-searches a sorted slice and returns the first index where the element is >= target.
What is the time complexity of sort.Search (binary search)?
- O(n)
- O(n log n)
- O(1)
- O(log n)
Answer: O(log n). Binary search runs in O(log n) but requires the slice to be sorted.
For a stable multi-key sort, how should the less func compare keys?
- Compare only the secondary key
- Compare the primary key first, fall through to the secondary only on ties
- Compare both keys with &&
- Order does not matter
Answer: Compare the primary key first, fall through to the secondary only on ties. Compare the primary key first and only break ties with the secondary key.
Why should a less func use strict < rather than <=?
- <= is a syntax error
- < is slower
- <= breaks the required strict weak ordering and can misbehave
- There is no difference
Answer: <= breaks the required strict weak ordering and can misbehave. The comparator must be a strict ordering; using <= can report both less(i,j) and less(j,i).