JavaScript Reference
Array.prototype.sort()
Sort elements in place. Accepts a comparator for custom ordering.
Last updated: October 2026
Definition: Array.prototype.sort() sorts the elements of an array in place and returns the same (mutated) array. By default it compares elements as strings in UTF-16 code unit order. Always pass a comparator when sorting numbers or objects.
🎬 Live Example
Five sort patterns. Click any button to see the output.
💡 sort mutates in place and returns the same array reference.
Basic Syntax
array.sort()
array.sort(compareFn)
// compareFn(a, b) should return:
// negative → a comes before b
// zero → keep order (stable)
// positive → a comes after b
🎯 Syntax & Parameters
| Parameter | Type | Description |
|---|---|---|
compareFn | Function | Optional. Returns negative/zero/positive to define order. |
a, b | any | Two elements being compared. |
The Default Sort Trap
// ⚠️ Default sorts lexicographically (as strings)
[10, 9, 1, 100, 25].sort();
// [1, 10, 100, 25, 9] ← wrong for numbers!
['banana', 'apple', 'cherry'].sort();
// ['apple', 'banana', 'cherry'] ✅ OK for strings
Always pass a comparator for numbers. Default string comparison puts "10" before "9".
Numeric Sort (Ascending / Descending)
const nums = [10, 9, 1, 100, 25];
// Ascending
nums.sort((a, b) => a - b);
// [1, 9, 10, 25, 100]
// Descending
nums.sort((a, b) => b - a);
// [100, 25, 10, 9, 1]
Case-Insensitive String Sort
const words = ['Banana', 'apple', 'Cherry'];
words.sort();
// ['Banana', 'Cherry', 'apple'] ← uppercase first
words.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
// ['apple', 'Banana', 'Cherry'] ✅
Pro tip: Use localeCompare for user-facing strings — it handles accents and language rules correctly.
Sorting Objects
const users = [
{ name: 'Bob', age: 30 },
{ name: 'Al', age: 25 },
{ name: 'Cara', age: 35 }
];
// By age
users.sort((a, b) => a.age - b.age);
// [{name:'Al',...}, {name:'Bob',...}, {name:'Cara',...}]
// By name
users.sort((a, b) => a.name.localeCompare(b.name));
// Multi-key: age first, then name
users.sort((a, b) => a.age - b.age || a.name.localeCompare(b.name));
Non-Destructive Sort
// ❌ Mutates
const arr = [3, 1, 2];
arr.sort((a, b) => a - b);
// ✅ Copy first
const original = [3, 1, 2];
const sorted = [...original].sort((a, b) => a - b);
// original: [3, 1, 2]
// sorted: [1, 2, 3]
// ✅ ES2023+ (modern)
const s2 = original.toSorted((a, b) => a - b);
🎯 sort vs toSorted vs reverse
| Method | Mutates? | Use when |
|---|---|---|
sort | Yes | In-place sort (fast, low memory) |
toSorted | No | Immutable sort (ES2023+) |
reverse | Yes | Flip order (not a sort) |
⚠️ Common Mistakes
- Calling sort() on numbers without a comparator. It sorts lexicographically — [10, 9].sort() gives [10, 9], not [9, 10].
- Assuming sort copies. It mutates. Use
[...arr].sort(). - Using
a > bas comparator. Must return a number. Usea - bfor numbers orlocaleComparefor strings. - Expecting case-insensitive sort. Default is case-sensitive — 'Z' comes before 'a'.
- Sorting objects without a key. Always pass
(a, b) => a.key - b.key. - Relying on unstable order pre-ES2019. Old engines could reorder equal elements. Modern engines are stable.
- Sorting before mapping in React. Don't mutate props. Use
[...arr].sort().
❓ Frequently Asked Questions
What does Array.sort() do in JavaScript?
Array.sort() sorts the elements of an array in place and returns the sorted array. By default it converts elements to strings and sorts lexicographically, which causes surprising results with numbers.
Why does [10, 9, 1].sort() return [1, 10, 9]?
By default, sort converts elements to strings and compares UTF-16 code units. '10' comes before '9' lexicographically. To sort numbers correctly, pass a comparator: (a, b) => a - b.
How do I sort numbers correctly?
Pass a numeric comparator: arr.sort((a, b) => a - b) for ascending or arr.sort((a, b) => b - a) for descending.
Does sort modify the original array?
Yes. sort mutates the array in place and returns the same array reference. To sort a copy, use [...arr].sort() or arr.slice().sort().
Is Array.sort() stable?
Yes. Since ES2019, sort is guaranteed stable. Elements that compare equal retain their original relative order.