Array.sort()
Sorts an array in place; use compareFn for correct numeric/object sorts.
array.sort(compareFn?)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Sorts an array in place; use compareFn for correct numeric/object sorts.
Array.prototype.sort() sorts the elements of an array in place and returns a reference to the same array. By default, sorting is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values. This default behavior often produces incorrect results for numbers (e.g., 10 comes before 2). For custom logic, provide a comparison function that returns a negative value if the first element should precede the second, a positive value if the second should precede the first, or zero to maintain relative order. Modern engines guarantee a stable sort (elements with equal keys stay in their original relative order). Performance typically follows O(n log n) time complexity. Because it mutates the original reference, developers should clone the array first using the spread operator if immutability is required. When comparing dates, use getTime() or ISO strings to ensure reliable chronological ordering.
Quick reference
Syntax
array.sort(compareFn?)
Inputs
Parameters
See it in practice
Examples
Numeric Ascending and Descending
var numbers = [40, 100, 1, 5, 25];
// Ascending
numbers.sort(function(a, b) {
return a - b;
});
console.log('Ascending:', numbers);
// Descending using spread to avoid mutating the ascending result
var descending = [...numbers].sort(function(a, b) {
return b - a;
});
console.log('Descending:', descending);Ascending: [1, 5, 25, 40, 100] Descending: [100, 40, 25, 5, 1]
Using a subtraction-based comparison function ensures numbers are treated as integers rather than strings. The spread operator is used to create a copy for the second sort.
Sorting Objects by Property
var items = [
{ name: 'Edward', value: 21 },
{ name: 'Sharpe', value: 37 },
{ name: 'And', value: 45 }
];
items.sort(function(a, b) {
var nameA = a.name.toUpperCase();
var nameB = b.name.toUpperCase();
if (nameA < nameB) return -1;
if (nameA > nameB) return 1;
return 0;
});[{ name: 'And', value: 45 }, { name: 'Edward', value: 21 }, { name: 'Sharpe', value: 37 }]
Sorts an array of objects based on a string property, using toUpperCase() to ensure case-insensitive alphabetical ordering.
Sorting Dates with getTime()
var events = [
{ name: 'Future', date: new Date('2025-12-25') },
{ name: 'Past', date: new Date('2010-01-01') },
{ name: 'Present', date: new Date('2023-05-15') }
];
events.sort(function(a, b) {
return a.date.getTime() - b.date.getTime();
});[{ name: 'Past', ... }, { name: 'Present', ... }, { name: 'Future', ... }]
For reliable date ordering, convert Date objects to numeric timestamps using getTime() before subtraction.
Debug faster
Common Errors
LogicError
Cause: Omitting the comparison function when sorting numbers results in lexicographical (string) sorting.
Fix: Always provide a compare function (a, b) => a - b for numeric arrays to avoid '10' coming before '2'.
var nums = [10, 2, 1];
nums.sort(); // [1, 10, 2]SideEffectError
Cause: Assuming sort() creates a new array, leading to unexpected mutations of the original variable which might be used elsewhere.
Fix: Create a shallow copy using the spread operator or slice() before calling sort().
var original = [3, 1, 2];
var sorted = original.sort();
console.log(original); // [1, 2, 3] - original is mutated!Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Sorts an array in place; use compareFn for correct numeric/object sorts.
compareFn: Comparator: (a, b) => number. Return <0, 0, >0.
LogicError: Always provide a compare function (a, b) => a - b for numeric arrays to avoid '10' coming before '2'. SideEffectError: Create a shallow copy using the spread operator or slice() before calling sort().