JavaScript Sort() Method
JS sort() method is used to rearrange the array elements alphabetically in ascending order. It updates the given array and does not return anything.
let arr = ["HTML", "CSS", "JS"];
arr.sort();
console.log(arr);
Output
[ 'CSS', 'HTML', 'JS' ]
Sorting Array of Strings in Descending Order
Modify the comparator for alphabetically reverse order.
let arr = ["HTML", "CSS", "JS"];
// sort with comparator function for reverse order
// Note : If we write a.localeCompare(b)), then it would
// sort in ascending order
arr.sort((a, b) => b.localeCompare(a));
console.log(arr);
Output
[ 'JS', 'HTML', 'CSS' ]
Case Insensitive Sorting of Strings
Convert the string to lowecase in the comparator function.
let arr = ["HTML", "html", "css", "CSS", "JS"];
// sort with comparator function for reverse order
arr.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
console.log(arr);
Output
[ 'css', 'CSS', 'HTML', 'html', 'JS' ]
Go through this, JavaScript String Sort, to learn more about sorting string in JS
Sorting Integers Array in JS
Ascending Order
Array.sort() sorts the elements in lexicographical order whether a string or number.
const arr = [ 10, 20, 25, 100 , 40]
console.log(arr.sort())
Output
[ 10, 100, 20, 25, 40 ]
The sorting with JavaScript's sort() is different from languages like C++ , Java and Python. It compares array elements alphabetically as strings rather than numbers. This causes elements to be sorted based on character Unicode values instead of numerical order.
To sort the numeric array propery we have to pass a comparator function.
const arr = [ 10, 20, 25, 100 , 40]
console.log(arr.sort((a,b) => a - b))
Output
[ 10, 20, 25, 40, 100 ]
Descending Order
change the comparator function to sort the array in descending order.
const arr = [ 10, 20, 25, 100 , 40]
console.log(arr.sort((a,b) => b - a))
Output
[ 100, 40, 25, 20, 10 ]
Please go through this How to Sort Numeric Array using JavaScript?, to know how the javascript array sort function works.
Sorting Array of Objects
The array of obects can be sorted on the basis of property values.
// Array of people objects with different names and ages
let people = [
{ name: 'Rahul', age: 28 },
{ name: 'Jatin', age: 25 },
{ name: 'Vikas', age: 32 },
{ name: 'Rohit', age: 35 }
];
// Sort the objects for age
people.sort((a, b) => a.age - b.age);
console.log(people);
// Sort object for names
people.sort((a, b) => a.name.localeCompare(b.name));
console.log(people);
Output
[ { name: 'Jatin', age: 25 }, { name: 'Rahul', age: 28 }, { name: 'Vikas', age: 32 }, { name: 'Rohit', age: 35 } ] [ { name: 'Jatin', age: 25 }, { name: 'Rahul', age: 28 }, { name: 'Rohit', age: 35 }, { name: 'Vikas', age: 32 } ]
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.