Stable Sort Definition Importance
Fast recall
Quick revision
Core answer
Whether you can define stability precisely and give a real reason it matters (multi-key sorting)
Confusing stability with correctness, or not knowing which common sorts are stable
Give the multi-key example: stable sort lets you sort by secondary then primary key
Interview-ready guide
Interview answer
A sort is stable if elements that are equal on the sort key keep their original relative order after sorting.
So if I'm sorting people by age and two people are both thirty, a stable sort guarantees they appear in the same order they were in the input, while an unstable sort is free to swap them. It's important to note this is not about correctness; both stable and unstable sorts produce fully sorted output. Stability only governs what happens to elements that tie on the key.
The reason it matters in practice is multi-key sorting. Say I want to sort employees primarily by department and, within each department, by name. The clean way is to sort by the secondary key, name, first, and then do a stable sort by the primary key, department.
Because the second sort is stable, it preserves the name ordering I established within each department, so I get the combined ordering I want. If that second sort were unstable, it would scramble the names within each department and ruin the effect. Stability also matters whenever the original input order itself carries meaning, like insertion or arrival time.
As for which sorts are stable: merge sort and insertion sort are stable, and so is TimSort, which is what Java uses for objects, and counting sort. Quicksort, heap sort, and selection sort are not stable. One last point: for primitive values stability is irrelevant, because two equal integers are genuinely identical, there's nothing to preserve.
In Java I'd usually express multi-key sorting in one stable step using Comparator dot comparing then thenComparing.
Stable Sort Definition Importance
Direct Answer
A stable sort preserves the relative order of equal elements. If two records compare equal on the sort key, a stable sort keeps them in their original order; an unstable sort may swap them. It matters for multi-key sorting (sort by one field, then another, without scrambling the first) and any time the original order carries meaning. Merge and insertion sort are stable; quick, heap, and selection are not.
Code Example
record Emp(String name, String dept) {}
List<Emp> list = new ArrayList<>(List.of(
new Emp("Ann", "Sales"), new Emp("Cara", "Eng"), new Emp("Bob", "Sales")));
// Step 1: sort by NAME (secondary key)
list.sort(Comparator.comparing(Emp::name));
// Step 2: STABLE sort by DEPT (primary key) keeps name order within a dept
list.sort(Comparator.comparing(Emp::dept));
// Result: Eng[Cara], Sales[Ann, Bob] <- Ann before Bob preserved
// (Comparator.thenComparing does this in one stable step too)
List.sort/Collections.sort use stable TimSort, so the first sort's order survives the second.
Common Mistakes & Pitfalls
- Stability == correctness: both produce sorted output; stability only fixes the order of equal keys.
- Wrong sort order for multi-key: sort the secondary key first, then the primary stably.
- Assuming quicksort is stable: it isn't; use merge/TimSort when order of equals matters.
- Primitives: stability is irrelevant for primitives (equal values are identical).
Check your understanding
Answer these without looking back at the article.
- Is Java's Collections.sort stable?
- How do you implement a multi-key sort using Comparator?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.