Sorting In Go
Fast recall
Quick revision
Core answer
Whether you understand Sorting In Go beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
The three forms, executed:
// 1. Simple
ints := []int{3, 1, 2}
sort.Ints(ints) // [1, 2, 3]
// 2. sort.Slice — any slice, any comparison
type User struct { Name string; Age int }
users := []User{{"Al", 30}, {"Bo", 25}}
sort.Slice(users, func(i, j int) bool {
return users[i].Age < users[j].Age // by Age
})
// 3. sort.Sort — for types implementing sort.Interface (Len, Less, Swap)
// (the old pattern; sort.Slice replaced it for most cases)The less function: sort.Slice's second argument answers 'is element i less than element j?' — the sort uses your comparison to order. The sort's stability: sort.Slice is NOT stable (equal elements can be reordered) — sort.SliceStable is the stable variant (when equal elements must keep their relative order).
The Go 1.21+ slices package (the future):
import "slices"
slices.Sort(users) // by a natural ordering (Ordered constraint)
slices.SortFunc(users, func(a, b User) int { return a.Age - b.Age }) // by comparisonThe complexity: O(n log n), in-place (the slice's memory is rearranged, not copied).
The reverse: sort.Slice with the flipped comparison, or sort.Reverse(...) for the interface form.
Sorting In Go
Direct Answer
sort.Ints / sort.Strings / sort.Float64s — the simple calls. sort.Slice(x, func(i,j int) bool { ... }) — custom comparator. The pattern is always: a 'less' function. Data must implement or be given the comparison.
Deep Dive & Architecture
The algorithm underneath: Go's sort uses pdqsort (pattern-defeating quicksort, Go 1.19+) — a hybrid (quicksort, insertion-sort for small ranges, heapsort as the fallback to avoid quicksort's O(n²) worst case). The pdqsort innovation: pattern detection (already-sorted runs, reverse-sorted runs) — beating the naive cases that plague plain quicksort.
The stability distinction in practice: users sorted by Age — two users with the same Age: sort.Slice may reorder them; sort.SliceStable preserves their original order. The practical impact: when the data has a prior order (a secondary sort key), the stable form composes the keys (sort by name first, then stable-sort by age — the name order survives within ages).
The slices package's significance (1.21+): generics enable the natural-ordering form (slices.Sort on any cmp.Ordered element) — no more Less functions for the common case. The ecosystem is migrating; the sort package remains forever — the two coexist.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Sorting In Go in a real project?
- How would you compare Sorting In Go with an alternative?
- What is a common mistake when using Sorting In Go?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.