JS sort() method is used to rearrange the array elements alphabetically in ascending order. It updates the given array and does not return anything.
JavaScript
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.
JavaScript
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.
JavaScript
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.
JavaScript
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.
JavaScript
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.
JavaScript
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.
JavaScript
// 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.
Similar Reads
JavaScript Array sort() Method The JS array.sort() method rearranges the array elements alphabetically and returns a sorted array. It does not create a new array but updates the original array.Sorting Array of StringsJavaScript// Original array let ar = ["JS", "HTML", "CSS"]; console.log(ar); // Sorting the array ar.sort() consol
4 min read
JavaScript Array toSorted() Method The JS arr.toSorted() method rearranges the array elements alphabetically and returns a new sorted array.Sorting Array of StringsJavaScriptconst arr = ["JS", "HTML", "CSS"]; const arr1 = arr.toSorted(); console.log(arr); console.log(arr1);Output[ 'JS', 'HTML', 'CSS' ][ 'CSS', 'HTML', 'JS' ]Array.toS
3 min read
JavaScript typedArray.sort() Method The typedArray.sort() is an inbuilt function in JavaScript which is used to sort the elements of the given typedArray and return the new typedArray with the sorted elements. Syntax: typedArray.sort(); Parameter: It does not accept any parameters. Return value: It returns the new typedArray with the
1 min read
JavaScript Intl Methods Intl Method: The Intl() method is used to format strings, numbers, dates, and times in local format with the help of constructors. Syntax: new Intl.constructors(locales, options); Parameters: locales argument: locales argument is used to determine the locale used in operation. locales may be :undefi
5 min read
SortedSet last() method in Java The last() method of SortedSet interface in Java is used to return the last i.e., the highest element currently in this set.Syntax: E last() Where, E is the type of element maintained by this Set.Parameters: This function does not accepts any parameter.Return Value: It returns the last or the highes
2 min read
JavaScript - Sort a Numeric Array Here are some common methods to Sort Numeric Array using JavaScript. Please note that the sort function by default considers number arrays same as an array of stings and sort by considering digits as characters.Using sort() with a Comparison Function - Most UsedThe sort() method in JavaScript, when
2 min read
SortedMap lastKey() method in Java The lastKey() method of SortedMap interface in Java is used to return the last or the greatest key currently in this map. Syntax: K lastKey() Where, K is the type of key maintained by this Set. Parameters: This function does not accepts any parameter. Return Value: It returns the last or the greates
2 min read
JavaScript TypedArray.prototype.toSorted() Method The toSorted() method, presented in ECMAScript 2023 or (ES2023) affords a secure and efficient manner of ordering elements of TypedArray in ascending order. The toSorted() method of TypedArray instances is the copying version of the sort() method, it gives back a new typed array with sorted elements
1 min read
Short compare() method in Java The compare() method of Short class is used to compare two primitive short values for numerical equality. As it is a static method therefore it can be used without creating any object of Short. Syntax: public static int compare(short x, short y) Parameters: This method accepts two parameters: x: whi
2 min read
Collect.js sort() Method The sort() method is used to sort the items of the collection. Syntax: collect(array).sort() Parameters: The collect() method takes one argument that is converted into the collection and then sort() method is applied on it. Return Value: This method return the collection after sorting. Below example
1 min read