Stable Sorting
Preserve equal-element order.
Stable Sorting is a free Go Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Stable Sorting
A stable sort keeps equal elements in their original relative order. If two records compare equal, the one that came first stays first.
Why It Matters
Stability matters when you sort by one field but want a previous order preserved for ties. For example, sort by city, keeping people alphabetical within each city.
sort.Slice Is Not Stable
The regular sort.Slice does not guarantee stability. Equal elements may be reordered. For guaranteed stability, use sort.SliceStable.
sort.SliceStable
sort.SliceStable has the same signature as sort.Slice but preserves the order of equal elements.
package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{3, 1, 2, 1}
sort.SliceStable(nums, func(i, j int) bool {
return nums[i] < nums[j]
})
fmt.Println(nums)
}Seeing Stability with Structs
Sort people by age. With a stable sort, people of equal age keep their input order.
package main
import (
"fmt"
"sort"
)
type Person struct {
Name string
Age int
}
func main() {
p := []Person{{"Ann", 30}, {"Bob", 25}, {"Cara", 30}}
sort.SliceStable(p, func(i, j int) bool {
return p[i].Age < p[j].Age
})
fmt.Println(p)
}Ann Before Cara
In the previous example Ann and Cara both have age 30. Because Ann appeared first in the input, a stable sort keeps Ann before Cara in the result.
Multi-Pass Sorting
Stability enables sorting in passes. Sort by the least important key first, then the most important. Each stable pass preserves the earlier order for ties.
package main
import (
"fmt"
"sort"
)
type Rec struct {
City string
Name string
}
func main() {
r := []Rec{{"Rome", "Zoe"}, {"Oslo", "Ann"}, {"Rome", "Ann"}}
sort.SliceStable(r, func(i, j int) bool { return r[i].Name < r[j].Name })
sort.SliceStable(r, func(i, j int) bool { return r[i].City < r[j].City })
fmt.Println(r)
}sort.Stable
For types implementing sort.Interface, use sort.Stable instead of sort.Sort to get stability.
package main
import (
"fmt"
"sort"
)
type ByLen []string
func (s ByLen) Len() int { return len(s) }
func (s ByLen) Less(i, j int) bool { return len(s[i]) < len(s[j]) }
func (s ByLen) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func main() {
w := []string{"bb", "cc", "a"}
sort.Stable(ByLen(w))
fmt.Println(w)
}Cost of Stability
Stable sorts may use slightly more memory or time than unstable ones. If you do not need to preserve equal-element order, plain sort.Slice is fine.
When to Choose Stable
Pick a stable sort when:
- Elements have meaningful original order
- You sort in multiple passes by different keys
- Equal items must not be shuffled
Stable Check Example
Two items with equal sort keys appear in input order A then B. A stable sort guarantees the output keeps A before B.
package main
import (
"fmt"
"sort"
)
func main() {
type T struct{ Key, Tag int }
ts := []T{{1, 100}, {1, 200}, {0, 300}}
sort.SliceStable(ts, func(i, j int) bool { return ts[i].Key < ts[j].Key })
fmt.Println(ts)
}Quick Check
You sort records by city using sort.SliceStable. Two records share the same city. What is guaranteed?
Recap
Stable sorting preserves equal-element order:
sort.SliceStablefor slicessort.Stablefor sort.Interface types- Enables multi-pass sorting by different keys
Frequently asked questions
Is the “Stable Sorting” lesson free?
Yes — the full text of “Stable Sorting” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Stable Sorting”?
Preserve equal-element order. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Stable Sorting” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Sorting Slices
- Custom Sort Orders
- Searching Sorted Data
- Stable Sorting