In this article, we will see how to use JavaScript’s sort() method to sort arrays of numbers, strings, and objects.
The sort() method in JavaScript allows us to sort the elements of an array in ascending order.
It sorts an array alphabetically. It changes the positions of the elements in the original array and returns the array itself.
By default, the sort() function sorts values as strings.
const months = ["March", "Jan", "Feb", "Dec"];
months.sort();
console.log(months);
// Output: ["Dec", "Feb", "Jan", "March"]
The sort() method converts the elements to strings and compares the strings to determine the order.
Let’s take a look at an example.
Example:
const numbers = [40, 100, 1, 5, 25, 10];
numbers.sort();
console.log(numbers); // [1, 10, 100, 25, 40, 5]
The result is not as expected, right?
In this example, the sort() method puts 100 before 25 because sort() converts the elements to strings and compares them.
And since the string “100” precedes the “25” when making a string comparison, we have the above result.
To fix this, we need to pass a comparison function to the sort() method. The sort() method will base on the return results of the comparison function (negative, positive) to determine the order of the elements.
array.sort((a,b) => {});
In it, the comparison function accepts two arguments, a and b.
The sort() method will sort the elements based on the return value of the function with the following rules:
Example
const numbers = [40, 100, 1, 5, 25, 10];
numbers.sort((a, b) => {
if (a > b) return 1;
if (a < b) return -1;
return 0;
});
console.log(numbers);
We can also write it cleaner:
const numbers = [40, 100, 1, 5, 25, 10];
numbers.sort((a, b) => a - b);
console.log(numbers)
// [1, 5, 10, 25, 40, 100]
const numbers = [40, 100, 1, 5, 25, 10];
numbers.sort((a, b) => b - a);
console.log(numbers)
// [100, 40, 25, 10, 5, 1]
Note: Because the sort() method sorts and modifies the array itself, we don’t need to create additional variables to store the results.
The sort() method is also used to sort arrays of strings.
const programming = ["java", "php", "python", "javascript", "full stack"];
programming.sort();
console.log(programming);
// ["full stack", "java", "javascript", "php", "python"]
To sort the array in descending order, you need to pass the comparison function to the sort() function as follows:
const programming = ["java", "php", "python", "javascript", "full stack"];
programming.sort((a, b) => {
if (a > b) return -1;
if (a < b) return 1;
return 0;
});
console.log(programming);
// ["python", "php", "javascript", "java", "full stack"]
The sort() function is case-sensitive.
So, to get the correct result, we must first convert the elements to the same lowercase string (or the same uppercase string).
const programming = ["javascript", "php", "java", "PHP"];
programming.sort(function(a, b) {
let x = a.toLowerCase();
let y = b.toLowerCase();
return x == y ? 0 : (x > y) ? 1 : -1;
});
console.log(programming);
// ["java", "javascript", "php", "PHP"]
For example, we have an array:
const people = [
{
name: "John Smith",
age: 28,
},
{
name: "Aio Doe ",
age: 25,
},
{
name: "Anna Lou",
age: 32,
},
{
name: "Peter Justin",
age: 45,
},
];
Sort by age:
people.sort((a, b) => a.age - b.age);
console.log(people);
Sort by name:
people.sort((a, b) => {
let x = a.name.toLowerCase();
let y = b.name.toLowerCase();
return x === y ? 0 : x > y ? 1 : -1;
});
console.log(people);