Table of Contents
What sort() Does
Reorders the array: it arranges the items in a new order.
Changes the original array: it sorts in place, so the array you called it on is modified.
Also returns the array: the same array, now sorted, not a copy.
Sorts as strings by default: every item is converted to text and compared alphabetically.
let fruits = ["mango", "apple", "banana"]; fruits.sort(); console.log(fruits); // ["apple", "banana", "mango"]
Syntax and the Compare Function
array.sort(compareFunction);
compareFunction: optional, but you'll want it for anything other than plain text.
It receives two items, a and b: it decides which one comes first.
Negative result: a goes before b.
Positive result: b goes before a.
Zero: their order stays the same.
Sorting Numbers Correctly
The default way goes wrong: numbers get treated as text, so 10 comes before 2.let nums = [10, 2, 33, 4]; console.log(nums.sort()); // [10, 2, 33, 4], wrongAscending order: subtract b from a.
let nums = [10, 2, 33, 4]; nums.sort((a, b) => a - b); console.log(nums); // [2, 4, 10, 33]Descending order: subtract a from b.
nums.sort((a, b) => b - a); console.log(nums); // [33, 10, 4, 2]
Sorting Strings
Plain sort works for simple cases: but uppercase letters come before lowercase ones.let names = ["vi", "Sam", "alex", "Bob"]; console.log(names.sort()); // ["Bob", "Sam", "alex", "vi"]Use localeCompare for proper alphabetical order:
names.sort((a, b) => a.localeCompare(b)); console.log(names); // ["alex", "Bob", "Sam", "vi"]It also handles accents and other languages better: which the default sort does not.
Sorting Objects
Sort by a number property:
let users = [
{ name: "Vi", age: 22 },
{ name: "Sam", age: 30 },
{ name: "Alex", age: 25 }
];
users.sort((a, b) => a.age - b.age);
console.log(users.map(u => u.name)); // ["Vi", "Alex", "Sam"]
Sort by a string property:
users.sort((a, b) => a.name.localeCompare(b.name)); console.log(users.map(u => u.name)); // ["Alex", "Sam", "Vi"]
Things to Remember
It changes the original array: copy it first if you need to keep the original order.let original = [3, 1, 2]; let sorted = [...original].sort((a, b) => a - b); console.log(original); // [3, 1, 2] console.log(sorted); // [1, 2, 3]There's a non-mutating version: toSorted() returns a new sorted array and leaves the original alone, in newer JavaScript environments.
let sorted2 = original.toSorted((a, b) => a - b);
Undefined values go to the end: and they never reach your compare function.
Sorting is stable in modern JavaScript: items that compare as equal keep their original relative order.
Common Mistakes
Sorting numbers without a compare function: this gives text-based order.
Writing a compare function that returns true or false: it must return a number, negative, zero or positive.
// Wrong nums.sort((a, b) => a > b); // Right nums.sort((a, b) => a - b);
Forgetting that it changes the original: copy the array first when the original order matters.
Conclusion
Frequently Asked Questions (FAQs)
2.Does sort() change the original array?Because it compares items as strings by default, and "10" comes before "2" alphabetically.
3.How do I sort in descending order?Yes. Use [...array].sort() or toSorted() to avoid that.
Use (a, b) => b - a for numbers, or b.localeCompare(a) for strings.
0 Comments