Sorting
Sorting puts a list into an order you define. In JavaScript, array.sort is an in-place, stable comparison sort that defaults to string order, not numeric order.
Detailed Explanation
arr.sort() mutates arr and also returns it. The default comparator converts each element with ToString and compares UTF-16. [10, 9, 1].sort() is [1, 10, 9] because "10" comes before "9". This is the first interview trap.
The comparator (a, b) => number must return a negative value if a should come before b, positive if a should come after, and 0 if they are tied. For numbers, (a, b) => a - b is ascending and (a, b) => b - a is descending. For strings, a.localeCompare(b) respects locale; a < b is UTF-16 and is usually wrong for user-facing names.
Since ES2019 the sort is stable: equal elements keep their original relative order. You can sort by last name and then by first name in two passes, or in one comparator that breaks ties.
sort is O(n log n) comparisons. The engine picks the algorithm (often Timsort-like). You do not pick. A comparator that mutates the array, or that returns NaN (undefined - 3), produces an unstable, engine-defined mess. Always return a finite number.
toSorted (ES2023) is the copy-and-sort method: the original array stays put. Prefer it when the array is shared. toReversed is the matching non-mutating reverse.
Sorting objects compares a key: (a, b) => a.due - b.due. When the key is a string date, compare Date.parse or ISO strings that already sort lexicographically (YYYY-MM-DD).
What it costs you: accidental mutation, lexicographic numbers, and O(n log n) when you only needed the minimum (Math.min(...arr) is O(n) but can blow the argument limit; a loop or reduce is safer). Sorting to find the median is fine; sorting on every keystroke of a 50k-row table wants a debounce or a worker.
Reach for sort with an explicit comparator whenever order matters. Reach for a heap when you need repeated min-extraction from a changing set. Reach for toSorted when callers still hold the original.
Code Examples
const nums = [10, 9, 1];
console.log([...nums].sort()); // => [ 1, 10, 9 ]
console.log([...nums].sort((a, b) => a - b)); // => [ 1, 9, 10 ]
const words = ["éclair", "apple", "Banana"];
console.log([...words].sort((a, b) => a.localeCompare(b, "en")));const players = [
{ name: "Ada", team: "red" },
{ name: "Bram", team: "blue" },
{ name: "Cleo", team: "red" },
];
// Stable: reds keep Ada then Cleo.
players.sort((a, b) => a.team.localeCompare(b.team));
console.log(players.map((p) => p.name)); // => [ 'Bram', 'Ada', 'Cleo' ]const original = [3, 1, 2];
const copy = original.toSorted((a, b) => a - b);
console.log(original); // => [ 3, 1, 2 ]
console.log(copy); // => [ 1, 2, 3 ]Common Gotchas
Default `sort` is string-based
numbers, dates, and mixed types need a comparator. `[10, 9].sort()` is not numeric order.
`sort` mutates
`const b = a.sort()` aliases `a`. Use `toSorted` or `[...a].sort(...)`.
A comparator that returns `NaN` or a boolean is undefined behaviour
return a number. Do not return `a > b`.
Spread of a huge array into `Math.min` can throw
`Math.min(...arr)` hits argument limits. Use a loop.