Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › JavaScript › Reference › sort()

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.

Input:

                
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
compareFnFunctionOptional. Returns negative/zero/positive to define order.
a, banyTwo elements being compared.
1

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".

2

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]
3

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.

4

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));
5

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
sortYesIn-place sort (fast, low memory)
toSortedNoImmutable sort (ES2023+)
reverseYesFlip order (not a sort)

⚠️ Common Mistakes

❓ 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.

🎯 What's Next?

← All JavaScript Reference