The sort()
method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
Example 1: Basic string sorting
// Example of sorting an array of strings
const fruits = ['banana', 'apple', 'cherry', 'date'];
fruits.sort();
console.log(fruits); // Output: ['apple', 'banana', 'cherry', 'date']
Explanation
When called without a compare function, sort()
arranges the array elements in alphabetical order. This works as expected for strings.
Example 2: The issue with sorting numbers
// Example showing the incorrect sorting of numbers by default
const numbers = [10, 5, 100, 2, 1];
numbers.sort();
console.log(numbers); // Output: [1, 10, 100, 2, 5]
Explanation
By default, sort()
converts numbers to strings before comparing. '100' comes before '2' in string comparison, leading to this incorrect numerical sort.
Example 3: Sorting numbers with a compare function
// Example of correctly sorting numbers using a compare function
const numbers = [10, 5, 100, 2, 1];
numbers.sort((a, b) => a - b); // Ascending order
console.log(numbers); // Output: [1, 2, 5, 10, 100]
Explanation
To sort numbers correctly, you must provide a compare function. If a - b
is negative, a
is sorted before b
. If it's positive, b
is sorted before a
.
Example 4: Sorting numbers in descending order
// Example of sorting numbers in descending order
const scores = [88, 95, 72, 100, 85];
scores.sort((a, b) => b - a); // Descending order
console.log(scores); // Output: [100, 95, 88, 85, 72]
Explanation
By reversing the logic in the compare function (b - a
), we can sort the array in descending order.
Example 5: Sorting an array of objects
// Example of sorting an array of objects by a property
const users = [
{ name: 'John', age: 30 },
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 35 }
];
users.sort((a, b) => a.age - b.age); // Sort by age
console.log(users);
// Output:
// [
// { name: 'Alice', age: 25 },
// { name: 'John', age: 30 },
// { name: 'Bob', age: 35 }
// ]
Explanation
The compare function can access properties of objects in the array. Here, we sort the users
array based on the age
property of each user object.
Example 6: Sorting strings with localeCompare()
// Example of sorting strings with accents and special characters
const items = ['réservé', 'premier', 'cliché', 'communiqué'];
items.sort((a, b) => a.localeCompare(b));
console.log(items); // Output: ['cliché', 'communiqué', 'premier', 'réservé']
Explanation
For strings with diacritics or from different languages, localeCompare()
provides more accurate sorting than the default sort()
.
Example 7: Sorting mixed-case strings
// Example of sorting strings with mixed cases
const words = ['Apple', 'banana', 'Cherry', 'apple'];
words.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
console.log(words); // Output: ['Apple', 'apple', 'banana', 'Cherry']
Explanation
To perform a case-insensitive sort, convert the strings to the same case (e.g., lowercase) within the compare function before comparing them.