Array.prototype.sort() sorts an array in place, but without a comparison function it converts items to strings and sorts them by UTF-16 code units, so [10, 9, 1].sort() gives [1, 10, 9]. Pass a comparator such as (a, b) => a - b for numbers, or use toSorted() (ES2023) to get a new sorted array and leave the original untouched.
Since ES2019 the sort is required to be stable, so items that compare equal keep their original order in every current browser and Node.js version.
The default comparison turns every element into a string, so "10" comes before "9". Two further rules apply: undefined values are always moved to the end, and holes in sparse arrays are left at the end as well.
A comparator receives two items and returns a negative number, zero or a positive number:
Subtraction is fine for finite numbers, but it returns NaN with NaN, Infinity - Infinity and mixed values. For data you do not control, write the comparison explicitly: (a, b) => (a < b ? -1 : a > b ? 1 : 0). For BigInt values, subtraction cannot be returned directly because the result must be a number, so compare with < and >.
localeCompare respects language rules, accents and case. For large arrays, create one Intl.Collator and reuse it because it is much faster than calling localeCompare repeatedly:
numeric: true sorts digit sequences by value, which gives natural order for file names.
The || falls through to the second key only when the first comparison returns 0. The result is oldest first, with ties ordered by name. Dates can be sorted by subtraction too, because Date objects convert to timestamps: events.sort((a, b) => a.date - b.date).
sort() changes the array it is called on and also returns that same array. This breaks React state and other immutable code. ES2023 added non-mutating methods:
| Mutating | Non-mutating (ES2023) |
|---|---|
arr.sort(cmp) | arr.toSorted(cmp) |
arr.reverse() | arr.toReversed() |
arr.splice(...) | arr.toSpliced(...) |
toSorted is available in current evergreen browsers and Node.js 20 and later. For older environments, copy first with [...original].sort(cmp). Typed arrays have their own sort and toSorted, and a typed array sorts numerically by default.
When computing the sort key is expensive, calculate it once per item instead of inside the comparator, which runs many times per element. This is the decorate, sort, undecorate pattern:
expensiveKey stands for any costly function, such as parsing a date from a string or reading a property through several layers. The comparator now does only a cheap numeric comparison. For small arrays the difference is negligible, so apply this only when profiling shows the comparator is a hot spot.
(a, b) => a > b. A comparator must return a number; booleans become 1 or 0 and never -1, giving inconsistent results.sort() mutates shared arrays.localeCompare on null or undefined values; filter or default them first.Yes. The specification has required a stable sort since ES2019, so equal items keep their relative order.
The specification does not mandate one. V8 uses TimSort, and the complexity is typically O(n log n).
Swap the operands in the comparator (b - a), or sort ascending and call reverse() (or toReversed()).
Subtract them (a.date - b.date) or compare getTime() values. ISO 8601 strings also sort correctly as plain strings.
Always give sort() a comparator unless you want string order. Use a - b for numbers, localeCompare or Intl.Collator for text, chain keys with ||, and prefer toSorted() when you must not mutate the original array.