Syntax
arr.sort((a, b) => a - b)
arr.reverse()Examples
The Default sort() Gotcha
Sorting numbers without a comparator produces unexpected results.
const numbers = [10, 1, 21, 2];
console.log(numbers.sort());
// [1, 10, 2, 21] - sorted as strings, not numbers!
console.log(numbers.sort((a, b) => a - b));
// [1, 2, 10, 21] - correct numeric ascending orderSorting with a Comparator
Using a comparator function for correct numeric and custom ordering.
const numbers = [5, 2, 8, 1, 9];
numbers.sort((a, b) => a - b); // ascending
console.log(numbers); // [1, 2, 5, 8, 9]
numbers.sort((a, b) => b - a); // descending
console.log(numbers); // [9, 8, 5, 2, 1]Sorting Objects and Reversing
Sorting an array of objects by a property, and reversing an array.
const people = [
{ name: "Charlie", age: 30 },
{ name: "Alice", age: 25 },
{ name: "Bob", age: 35 }
];
people.sort((a, b) => a.age - b.age);
console.log(people.map(p => p.name)); // ["Alice", "Charlie", "Bob"]
const letters = ["a", "b", "c"];
letters.reverse();
console.log(letters); // ["c", "b", "a"]Best practices
- Always provide a comparator function when sorting numbers - the default string-based sort produces incorrect ordering
- Remember sort() and reverse() both mutate the original array in place - copy it first with [...arr].sort() if you need to preserve the original
- For sorting objects, subtract the numeric property (a.value - b.value) for ascending order, or swap the order for descending
- Use localeCompare() as the comparator when sorting strings that may contain accented characters, for correct alphabetical ordering
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Array Basics
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().