Sort Array in descending order JavaScript | 3 Ways code

You can use the sort() method to sorts the values as strings in alphabetical and descending order in JavaScript. By default this method sorts values in ascending order.

Note: This method changes the original array.

Example of Sort Array in descending order JavaScript

HTML Example code Sort numbers in an array in descending order:

Output:

Sort Array in descending order JavaScript

What is a way to sort strings in descending order in JavaScript?

Answer: There a many ways to sort string in JavaScript, we are trying only with 3 most common ways:-

Or

Or

The performance winner is : obj.sort().reverse().

Testing with an array of 10.000 elements, obj.sort().reverse() is faster than obj.sort( function ) (except on chrome), and obj.sort( function ) (using localCompare).

Performance test here :

Output:

efficient sort strings in descending order in JavaScript

Source: stackoverflow…

Do comment if you know much better example or have any doubts on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.