sort() reorders the elements of an array in place. Without a compare function, JavaScript converts elements to strings and sorts them by UTF-16 code-unit order, so numeric arrays usually need an explicit comparator.
const numbers = [2, 22, 4, 24, 32];
numbers.sort((a, b) => a - b);
console.log(numbers); // [2, 4, 22, 24, 32]
array.sort()
array.sort(compareFn)
The optional compareFn(a, b) controls the order. If it is omitted, values are converted to strings for comparison. sort() returns the same array reference after sorting.
The original page began with a simple alphabetical example:
const values = ["lmn", "ABC", "qwt"];
values.sort();
console.log(values);
["ABC", "lmn", "qwt"]
Default string ordering is based on UTF-16 code units, not a human-language dictionary order. Capitalization and accented characters can therefore affect the result.
The original page correctly showed that default sorting does not perform numeric comparison:
const numbers = [2, 22, 4, 24, 32];
numbers.sort();
console.log(numbers);
[2, 22, 24, 32, 4]
The values are compared as strings, so "32" comes before "4".
const numbers = [2, 22, 4, 24, 32];
const ascending = [...numbers].sort((a, b) => a - b);
const descending = [...numbers].sort((a, b) => b - a);
console.log(ascending); // [2, 4, 22, 24, 32]
console.log(descending); // [32, 24, 22, 4, 2]
Try the numeric ascending/descending demo.
The compare function receives two elements, commonly named a and b. Its return value determines their relative order.
a comes before b.a comes after b.NaN): the values are treated as equal for ordering.function compareNumbers(a, b) {
return a - b;
}
const numbers = [2, 22, 4, 24, 32];
numbers.sort(compareNumbers);
console.log(numbers); // [2, 4, 22, 24, 32]
This is the modern simplified equivalent of the longer compare function used on the original page.
The original page also sorted programming-language names by element length. That is a separate criterion from alphabetical order.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
scripts.sort((a, b) => a.length - b.length);
console.log(scripts); // ["PHP", "ASP", "HTML", "JavaScript"]
When lengths are equal, a stable sort preserves the earlier relative order of those equal-length values.
Try the string-length sorting demo.
For user-facing text, localeCompare() is usually better than forcing strings to uppercase or lowercase and comparing code units manually.
const names = ["Émile", "alice", "Zoë", "bob"];
names.sort((a, b) => a.localeCompare(b, undefined, { sensitivity: "base" }));
console.log(names);
The exact order can depend on the locale and options you request.
Try the basic alphabetical sorting demo.
const products = [
{ name: "Book", price: 250 },
{ name: "Pen", price: 20 },
{ name: "Bag", price: 800 }
];
products.sort((a, b) => a.price - b.price);
console.log(products);
For text properties, use a string comparator such as a.name.localeCompare(b.name).
const original = [3, 1, 2];
const sorted = original.sort((a, b) => a - b);
console.log(original); // [1, 2, 3]
console.log(sorted === original); // true
const original = [3, 1, 2];
const sorted = original.toSorted((a, b) => a - b);
console.log(original); // [3, 1, 2]
console.log(sorted); // [1, 2, 3]
toSorted() is the modern copying counterpart to sort(). A shallow-copy fallback is [...original].sort(compareFn).
Modern ECMAScript requires sort() to be stable: when the comparator considers two elements equal, their previous relative order is preserved.
const students = [
{ name: "Alex", grade: 15 },
{ name: "Devlin", grade: 15 },
{ name: "Sam", grade: 14 }
];
students.sort((a, b) => a.grade - b.grade);
console.log(students);
Alex remains before Devlin because both have the same grade and were already in that order.
With default sorting, undefined values move to the end without being passed to the compare function. Empty slots in sparse arrays are preserved and also move toward the end.
const values = ["b", undefined, "a"];
values.sort();
console.log(values); // ["a", "b", undefined]
Alphabetical Sort Demo Sort by Length Numeric Ascending / Descending
You can also use reverse() to reverse an existing order, although a direct descending comparator is clearer for numeric sorting.
sort() on numbers without a numeric comparator.sort() mutates the original array.true/false instead of negative/positive/zero-style values.sort().reverse() when a descending comparator directly expresses the order.Array Reference Display Array Elements
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.