🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Array.sort()

AI & DATA SCIENCE // sort

sort() sorts array elements in place. Without a comparator, it converts to strings and sorts lexicographically. Provide a comparator for numeric or custom sorting.

Syntax

// Lexicographic (default - often wrong for numbers!)
array.sort();

// Numeric sort
array.sort((a, b) => a - b);

Deep Dive Course

**sort()** mutates the original array and returns it. The default sort converts elements to strings and sorts by UTF-16 code units — this gives wrong results for numbers (`[10, 9, 2].sort()` → `[10, 2, 9]`). Always provide a **comparator function** `(a, b) => a - b` for numbers. The sort is stable in modern JS engines.

1Understanding Array.sort()

sort() mutates the original array and returns it. The default sort converts elements to strings and sorts by UTF-16 code units — this gives wrong results for numbers ([10, 9, 2].sort()[10, 2, 9]). Always provide a comparator function (a, b) => a - b for numbers. The sort is stable in modern JS engines.

💡

sort() MUTATES the original array. Use [...arr].sort() to sort a copy if you need to preserve the original.

editor.html
// WRONG: default sort on numbers
const nums = [10, 9, 2, 1, 11];
console.log([...nums].sort()); // [1, 10, 11, 2, 9] WRONG!

// CORRECT: numeric comparator
console.log([...nums].sort((a, b) => a - b)); // [1, 2, 9, 10, 11]
localhost:3000

2Practical Example

Here is a real-world application of Array.sort() showing how it is used in production JavaScript code.

editor.html
// Sort objects by property
const people = [
  { name: 'Charlie', age: 35 },
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 30 },
];
people.sort((a, b) => a.age - b.age);
console.log(people.map(p => p.name)); // ['Alice','Bob','Charlie']
localhost:3000

3Best Practices

Follow these guidelines when working with Array.sort():

1. Always provide a comparator for numeric sorting

2. Use [...arr].sort() to avoid mutating the original

3. For stable sort of objects, include all sort keys in the comparator

⚠️

Tip: sort() MUTATES the original array. Use [...arr].sort() to sort a copy if you need to preserve the original.

editor.html
// WRONG: default sort on numbers
const nums = [10, 9, 2, 1, 11];
console.log([...nums].sort()); // [1, 10, 11, 2, 9] WRONG!

// CORRECT: numeric comparator
console.log([...nums].sort((a, b) => a - b)); // [1, 2, 9, 10, 11]
localhost:3000

Examples

Example 01Basic Usage
// WRONG: default sort on numbers
const nums = [10, 9, 2, 1, 11];
console.log([...nums].sort()); // [1, 10, 11, 2, 9] WRONG!

// CORRECT: numeric comparator
console.log([...nums].sort((a, b) => a - b)); // [1, 2, 9, 10, 11]
Example 02Advanced Example
// Sort objects by property
const people = [
  { name: 'Charlie', age: 35 },
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 30 },
];
people.sort((a, b) => a.age - b.age);
console.log(people.map(p => p.name)); // ['Alice','Bob','Charlie']

Best Practices

  • Always provide a comparator for numeric sorting
  • Use [...arr].sort() to avoid mutating the original
  • For stable sort of objects, include all sort keys in the comparator

Interview Question

Why is the default sort() wrong for numbers?

Hint: String comparison vs numeric comparison.

The default sort() converts elements to strings and sorts by UTF-16 code units. '10' < '2' because '1' < '2' lexicographically. So [10, 2, 1].sort() gives [1, 10, 2] — wrong! Use sort((a, b) => a - b) for ascending numeric sort.

Exercises

MediumPractice using Array.sort() in a real scenario.
View Solution
// WRONG: default sort on numbers
const nums = [10, 9, 2, 1, 11];
console.log([...nums].sort()); // [1, 10, 11, 2, 9] WRONG!

// CORRECT: numeric comparator
console.log([...nums].sort((a, b) => a - b)); // [1, 2, 9, 10, 11]

Frequently Asked Questions

Why is the default sort() wrong for numbers?

The default sort() converts elements to strings and sorts by UTF-16 code units. '10' < '2' because '1' < '2' lexicographically. So [10, 2, 1].sort() gives [1, 10, 2] — wrong! Use sort((a, b) => a - b) for ascending numeric sort.

Related Functions

Array-Methodsfiltermap