Array.prototype.sort()
sort() orders an array in place. By default it sorts elements as strings; pass a compare function to sort numbers or objects correctly.
Definition
Array.prototype.sort() sorts the elements of an array in place and returns that same array. With no argument it converts elements to strings and sorts them by their UTF-16 code units — which is why [1, 10, 2].sort() gives [1, 10, 2], not [1, 2, 10]. To sort meaningfully you almost always pass a compare function.
Syntax
array.sort()
array.sort(compareFn)
array.sort((a, b) => {
// return < 0 → a comes before b
// return > 0 → b comes before a
// return 0 → keep a and b in their current order
})
Parameters
| Parameter | Required | Description |
|---|---|---|
compareFn | No | Defines the sort order. Omit it to sort elements as strings by UTF-16 code unit. |
a | — | The first element being compared. |
b | — | The second element being compared. |
The compare function must return a negative number if a should come first, a positive number if b should come first, and 0 to leave their relative order unchanged. For numbers, (a, b) => a - b satisfies this directly.
Return value
The same array, now sorted. sort() mutates in place — it does not return a copy — so the original variable and any other reference to it see the new order.
- Mutates the array and returns the same reference.
- The sort is stable: elements the compare function treats as equal (
0) keep their original relative order. Modern engines have guaranteed this since 2019. undefinedelements are always moved to the end and are never passed tocompareFn.- Empty slots in a sparse array are sorted to the end (after any
undefined) and stay empty.
Examples
The default sort is by string order
The compare function decides the order
// Ascending numbers
[4, 2, 5, 1].sort((a, b) => a - b); // [1, 2, 4, 5]
// Descending numbers
[4, 2, 5, 1].sort((a, b) => b - a); // [5, 4, 2, 1]
// Objects by a field
const users = [{ name: 'Ada', age: 36 }, { name: 'Bo', age: 22 }];
users.sort((a, b) => a.age - b.age); // Bo, then Ada
Stability keeps equal elements in order
const rows = [
{ team: 2, name: 'A' },
{ team: 1, name: 'B' },
{ team: 2, name: 'C' },
{ team: 1, name: 'D' },
];
rows.sort((a, b) => a.team - b.team);
// team 1: B then D (original order kept), then team 2: A then C
console.log(rows.map(r => r.name)); // ['B', 'D', 'A', 'C']
Sort without mutating (toSorted)
const original = [3, 1, 2];
// classic: copy first
const copy = [...original].sort((a, b) => a - b);
// modern (2023+): a built-in non-mutating version
const sorted = original.toSorted((a, b) => a - b);
console.log(original); // [3, 1, 2] — untouched in both approaches
Browser & runtime support
Sorting strings the way humans expect
The default sort places capital letters before lowercase ones ('B' < 'a' by code unit) and doesn't understand accents or number-in-string ordering. Use localeCompare for human-friendly results:
['item2', 'item10', 'item1'].sort();
// ['item1', 'item10', 'item2'] ← '1' < '2' character by character
['item2', 'item10', 'item1'].sort((a, b) =>
a.localeCompare(b, undefined, { numeric: true })
);
// ['item1', 'item2', 'item10'] ← natural numeric order
Availability
sort() has existed since the first edition of JavaScript; its stable behavior has been guaranteed across engines since 2019. The non-mutating toSorted() is newer (2023) and available in current browsers and Node.js — copy-then-sort remains the compatible fallback.
Frequently asked questions
Does sort() return a new array?
[...array].sort(...) or toSorted() if you need to keep the original.Why is [1, 10, 2].sort() in the wrong order?
(a, b) => a - b to sort numbers.