What Does Array Sort Stability Mean?
Understand what stable sorting means in JavaScript, why ES2019 guarantees it, and how to rely on it for tiebreaking.
Expected Interview Answer
A sort is stable when elements that compare as equal under the comparator retain their original relative order in the output, and modern JavaScript engines guarantee Array.prototype.sort is stable per the ECMAScript 2019 specification.
Before ES2019, engines were free to implement sort however they liked, so V8 famously used an unstable algorithm for arrays longer than 10 elements, which meant equal-ranked items could silently be reordered. Since ES2019, the spec mandates stability, so all conforming engines β V8, SpiderMonkey, JavaScriptCore β must preserve the relative order of equal elements regardless of array size. This matters most when sorting by one field but wanting a secondary implicit order preserved, like sorting a list of tasks by priority while keeping tasks with the same priority in their original insertion order. Practically, you should still write comparators defensively (returning a proper negative/zero/positive number, not just booleans) and, when multiple sort keys matter, either chain comparisons explicitly or rely on stability rather than assuming it without checking your target runtime.
- Guaranteed by spec since ES2019, so behavior is now predictable across engines
- Equal elements never get silently shuffled, simplifying secondary-order reasoning
- Lets you sort by one field while trusting insertion order as an implicit tiebreaker
- Enables multi-pass stable sorts: sort by secondary key first, then primary key
AI Mentor Explanation
Stability means that when you re-sort a scorecard by runs scored and two batsmen tied on the same score, they stay listed in the order they originally batted rather than getting shuffled arbitrarily. An unstable sort could swap their listed order every time you ran it, even though their scores are identical. A stable sort guarantees the batting-order tiebreaker survives the re-sort untouched. That preserved relative order among ties is exactly what sort stability guarantees.
Step-by-Step Explanation
Step 1
Define equal elements
Two elements are βequalβ when the comparator returns 0 for the pair.
Step 2
Check the guarantee
Since ES2019, Array.prototype.sort is spec-guaranteed stable in all conforming engines.
Step 3
Rely on it for secondary order
Sort by a primary key while trusting existing array order to act as an implicit secondary key.
Step 4
Chain explicitly when needed
For multiple explicit sort keys, compare the primary key first and fall back to a secondary comparison inside the same comparator.
What Interviewer Expects
- Correct definition: equal elements keep their relative order
- Knowledge that ES2019 made Array.prototype.sort stability a spec requirement
- Awareness that pre-ES2019 V8 was unstable for arrays over 10 elements
- Practical example of using stability for secondary-key ordering
Common Mistakes
- Assuming all sorts, including custom algorithms like quicksort, are inherently stable
- Not knowing modern JS engines guarantee stability since ES2019
- Writing comparators that return booleans instead of negative/zero/positive numbers
- Ignoring stability when relying on insertion order as an implicit tiebreaker
Best Answer (HR Friendly)
βA stable sort means that if two items are considered equal by whatever you are sorting on, they stay in the same order relative to each other as before the sort. Modern JavaScript guarantees this, so you can safely sort by one thing and trust that ties keep their original order.β
Code Example
const tasks = [
{ id: 1, priority: 2, title: 'Fix bug' },
{ id: 2, priority: 1, title: 'Write docs' },
{ id: 3, priority: 2, title: 'Add tests' },
{ id: 4, priority: 1, title: 'Review PR' },
]
// Stable sort: ties (same priority) keep original relative order
const sorted = [...tasks].sort((a, b) => a.priority - b.priority)
// priority 1: 'Write docsβ then 'Review PR' (original order preserved)
// priority 2: 'Fix bug' then 'Add tests' (original order preserved)Follow-up Questions
- How would you sort by multiple keys explicitly in one comparator?
- Why did V8 change its sort implementation around ES2019?
- What is the time complexity of a typical stable sort like TimSort?
- How do you sort an array of objects without mutating the original array?
MCQ Practice
1. What does it mean for a sort algorithm to be stable?
Stability specifically concerns preserving relative order among elements considered equal.
2. Since which ECMAScript version is Array.prototype.sort guaranteed stable by spec?
The ECMAScript 2019 specification mandated that Array.prototype.sort be a stable sort.
3. What should a sort comparator return, not just a boolean, to work correctly?
The comparator must return a number indicating relative order; booleans coerce incorrectly.
Flash Cards
Stable sort definition? β Elements comparing as equal keep their original relative order.
Is Array.prototype.sort stable today? β Yes, guaranteed since the ES2019 specification.
Pre-ES2019 V8 behavior? β Unstable for arrays longer than 10 elements.
Comparator return type? β A number: negative, zero, or positive β never a plain boolean.