What Algorithm Does JavaScript Array.sort() Use?
Array.prototype.sort() is one of the most-used methods in JavaScript, but its behavior has evolved and varies by engine. Here is what actually runs when you call it, and the one rule that saves you from the most common bug.
The engine implementations
V8 (Chrome, Node.js) switched to Tim Sort in 2018. Firefox's SpiderMonkey uses a Merge Sort variant. The ECMAScript spec does not mandate an algorithm, but since ES2019 it requires the sort to be stable, so all modern engines preserve the order of equal elements.
The numeric gotcha
The default sort compares elements as strings, so [10, 9, 100].sort() gives [10, 100, 9]. Always pass a comparator for numbers: arr.sort((a, b) => a - b). See more JS examples in sorting algorithms in JavaScript.
Frequently asked questions
Is JavaScript's Array.sort stable? +
What algorithm does V8 use for sorting? +
See it in motion
Watch this algorithm and 9 others run step by step in our free interactive visualizer.
▶ Launch Visualiser