Array.prototype.sort causes common production bugs because it mutates in place, compares as strings by default, and depends on a correct comparator for stable domain-specific ordering.
Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.
Array.sort Pitfalls in JavaScript (Lexicographic Sorting, Mutation, Comparator)Frontend interview answer
This JavaScript interview question tests whether you can explain JavaScript Array.sort pitfalls: mutation, lexicographic order, and comparator bugs, connect it to production trade-offs, and handle common follow-up questions.
- JavaScript Array.sort pitfalls: mutation, lexicographic order, and comparator bugs explanation without falling back to memorized docs wording
- Arrays and Sorting reasoning, edge cases, and production failure modes
- How you would answer the most likely JavaScript interview follow-up
Production pitfall
Array.prototype.sort() looks harmless but it mutates the original array and sorts values as strings by default. That is why numeric lists, shared state, and custom ordering logic fail in ways that are frustrating to debug.
const nums = [1, 2, 10, 21];
nums.sort();
console.log(nums); // [1, 10, 2, 21] ❗ (string comparison)
The correct numeric sort
const nums = [1, 2, 10, 21];
nums.sort((a, b) => a - b);
console.log(nums); // [1, 2, 10, 21] ✅
Pitfall #1: It mutates the original array
sort() sorts in place. If you’re in React/Angular/Redux and expect immutability, this can silently corrupt your state.
const original = [3, 1, 2];
const sorted = original.sort((a, b) => a - b);
console.log(original); // [1, 2, 3] ❗ mutated
console.log(sorted === original); // true
Safe pattern:
const original = [3, 1, 2];
const sorted = [...original].sort((a, b) => a - b);
console.log(original); // [3, 1, 2] ✅
console.log(sorted); // [1, 2, 3]
Pitfall #2: Wrong comparator logic
The comparator must return:
- < 0 if
ashould come beforeb
- > 0 if
ashould come afterb
- 0 if equal
Returning true/false is a bug.
// ❌ Buggy comparator
arr.sort((a, b) => a > b);
// ✅ Correct
arr.sort((a, b) => a - b);
Pitfall #3: Sorting objects
You must compare a specific field:
const users = [
{ name: 'Bob', age: 30 },
{ name: 'Alice', age: 25 },
{ name: 'Carol', age: 35 }
];
// By age
users.sort((a, b) => a.age - b.age);
// By name (locale-safe)
users.sort((a, b) => a.name.localeCompare(b.name));
Stability note
Modern JavaScript engines (ES2019+) guarantee that sort is stable, but older engines did not. In interviews, it’s good to mention that today it is stable by spec.
One-sentence answer
Array.sort() sorts in place and compares items as strings by default, so you must clone the array for immutability and provide a correct numeric or custom comparator to avoid subtle bugs.
Practical scenario
Sort prices or dates before rendering a table; a numeric comparator is required to avoid lexicographic order.
Common pitfalls
- Forgetting
sortmutates the original array. - Using an inconsistent comparator that breaks ordering.
- Assuming default sort is numeric.
Copy before sorting to keep immutability. Test with negative numbers and verify stability for performance-sensitive lists.
Use this as one explanation rep, then continue with the JavaScript interview questions cluster or a guided prep path.