How to sort an array in JavaScript
sort() reorders the elements of an array. By default, it sorts values as strings, which produces unexpected results for numbers. You almost always need to pass a comparison function.
Warning: sort() mutates the original array. If you want to keep the original, sort a copy first: [...array].sort(...).
Sorting strings
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months); // ['Dec', 'Feb', 'Jan', 'March']
The default sort works well for simple string arrays.
Sorting numbers
Sorting numbers without a comparison function gives wrong results, because sort() converts values to strings first:
const numbers = [40, 100, 1, 5, 25, 10];
numbers.sort();
console.log(numbers); // [1, 10, 100, 25, 40, 5] (wrong order)
Pass a comparison function to fix this:
// Ascending
const numbers = [40, 100, 1, 5, 25, 10];
numbers.sort((a, b) => a - b);
console.log(numbers); // [1, 5, 10, 25, 40, 100]
// Descending
const numbers = [40, 100, 1, 5, 25, 10];
numbers.sort((a, b) => b - a);
console.log(numbers); // [100, 40, 25, 10, 5, 1]
The comparison function receives two elements a and b. Return a negative number to sort a before b, a positive number to sort b before a, or zero to leave them in place.
Sorting without mutating the original
Because sort() changes the original array, sort a copy when you need to preserve the original:
const original = [40, 100, 1, 5, 25, 10];
const sorted = [...original].sort((a, b) => a - b);
console.log(sorted); // [1, 5, 10, 25, 40, 100]
console.log(original); // [40, 100, 1, 5, 25, 10] (unchanged)
Case-insensitive string sorting
The default string sort is case-sensitive, so capital letters sort before lowercase letters. Use localeCompare() for a cleaner case-insensitive sort:
const languages = ['javascript', 'PHP', 'Java', 'python'];
const sorted = [...languages].sort((a, b) =>
a.localeCompare(b, undefined, { sensitivity: 'base' })
);
console.log(sorted); // ['Java', 'javascript', 'PHP', 'python']
localeCompare() also handles accented characters and language-specific rules more reliably than manual .toLowerCase() comparisons.
Sorting an array of objects
Sort by a numeric property:
const people = [
{ name: 'John Smith', age: 28 },
{ name: 'Aio Doe', age: 25 },
{ name: 'Anna Lou', age: 32 },
{ name: 'Peter Justin', age: 45 },
];
const byAge = [...people].sort((a, b) => a.age - b.age);
console.log(byAge.map(p => p.name)); // ['Aio Doe', 'John Smith', 'Anna Lou', 'Peter Justin']
Sort by a string property:
const byName = [...people].sort((a, b) =>
a.name.localeCompare(b.name)
);
console.log(byName.map(p => p.name)); // ['Aio Doe', 'Anna Lou', 'John Smith', 'Peter Justin']
Descending string sort
To reverse alphabetical order, flip the arguments to localeCompare():
const programming = ['java', 'php', 'python', 'javascript', 'full stack'];
const descending = [...programming].sort((a, b) => b.localeCompare(a));
console.log(descending); // ['python', 'php', 'javascript', 'java', 'full stack']