Skip to content

JavaScript sort descending | Example code

  • by

Use the JavaScript sort() method with the reverse() method in the Array object to get sort elements in descending. This method sorts the elements of an array and overwrites the original array.


The reverse() method reverses the order of the elements in an array and overwrites the original array.

JavaScript sort descending example

Simple example code.

<!DOCTYPE html>

    const fruits = ["Banana", "Orange", "Apple", "Mango"];

    var arr = ["a","b","c","A","B","Z"];
    arr.sort((a, b) => b.localeCompare(a));

    let num = [0, 1 , 2, 3, 10, 20, 30 ];
    num.sort().sort((a, b) => b - a);



JavaScript sort descending

Other methods

Using the sort compare function

obj.sort((a, b) => (a > b ? -1 : 1))

Using localeCompare

obj.sort((a, b) => b.localeCompare(a) )

The performance

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).


Comment if you have any doubts or suggestions on this Js sorting topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *