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 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
Stream sorted() in Java Stream sorted() returns a stream consisting of the elements of this stream, sorted according to natural order. For ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed. It is a stateful intermediate operation i.e, it may incorporate state from previously s
2 min read
C# | How to create a SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. Properties : In C#, SortedSet class can be used to store, remove or view elements. It maintains ascending order and does not store duplicate elements. It is suggeste
2 min read
PriorityQueue comparator() Method in Java The java.util.PriorityQueue.comparator() method shares an important function of setting and returning the comparator that can be used to order the elements in a PriorityQueue. The method returns a null value if the queue follows the natural ordering pattern of the elements.Syntax: comp_set = (Priori
2 min read
Stream sorted (Comparator comparator) method in Java Stream sorted(Comparator comparator) returns a stream consisting of the elements of this stream, sorted according to the provided Comparator. For ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed. It is a stateful intermediate operation i.e, it may inco
3 min read
SortedMap comparator() method in Java with Examples The comparator() method of java.util.SortedMap interface is used to return the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.Syntax: public Comparator comparator() Return Value: This method returns the comparator used to order the keys in th
2 min read
Javascript Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples:Input: {0, 1, 2, 0, 1, 2}Output: {0, 0, 1, 1, 2, 2}Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}Output: {0, 0, 0, 0, 0
5 min read
PriorityBlockingQueue comparator() method in Java The comparator() method of PriorityBlockingQueue returns the comparator that can be used to order the elements in a PriorityBlockingQueue. The method returns null value if the queue follows the natural ordering pattern of the elements. Syntax: public Comparator<? super E> comparator() Returns:
2 min read
Analysis of different sorting techniques In this article, we will discuss important properties of different sorting techniques including their complexity, stability and memory constraints. Before understanding this article, you should understand basics of different sorting techniques (See : Sorting Techniques). Time complexity Analysis - W
5 min read
TreeSet comparator() Method in Java with Examples TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to co
3 min read