Sort An Array In Descending Order In JavaScript

You can sort an array in descending order in JavaScript using any of the following methods –

1. Use the Array.sort().reverse() method – You can first sort the array in ascending order using Array.sort() method and then reverse it using the Array.reverse() method to get the elements in descending order.
2. Use the … operator with Array.sort() method – The three dots (…) is used as spread operator here which expands the elements of the array and then sorts them in descending order using the default compare function.

Let’s look at each of these methods in detail below.

Contents

Sort An Array In Descending Order In JavaScript

1) Use The Array.sort().reverse() Method

You can use the Array.sort().reverse() method to sort an array in descending order in JavaScript. This is because, by default, the Array.sort() method sorts the element in ascending order. So, if you reverse it using the Array.reverse() method, it will sort the elements in descending order.

The syntax of the Array.sort().reverse() method is –

``array.sort().reverse()``

Let’s take an example to understand this –

``````// sort array in descending order

let fruits = ["apple", "orange", "mango", "banana"];

fruits.sort().reverse();

console.log(fruits); // ["orange", "mango", "banana", "apple"]``````

In the above code, we declared an array of fruits. We then used the Array.sort() method to sort the array in ascending order. We then used the reverse() method on the array to reverse the sorted array to get the array in descending order.

We can also use a comparator method inside the sort method to sort the array in descending order. The following is the syntax of using a comparator method with the Array.sort() method to sort an array in descending order –

``array.sort((a, b) => a>b?-1:1); // for primitive types``

The following is an example to understand this –

``````let fruits = ["apple", "orange", "mango", "banana"];

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

console.log(fruits); // ["orange", "mango", "banana", "apple"]``````

In the above code, we declared an array of fruits. We then used the Array.sort() method with a comparator function that returns -1 only when the value of ‘a’ is greater than ‘b’. This causes the sort to happen in reverse order i.e. from greatest to smallest.

The above methods sort the array in place. In my personal experience, it’s better to let the original array as it is and return a new array that is sorted in descending order. This will make the code much less messy.

2) Use The … Operator With Array.sort() Method

You can also use the spread (…) operator with the Array.sort() method to sort the array in descending order if you do not want to change the original array.

The syntax of using the spread operator with the Array.sort() method is –

``````let fruits = ["apple", "orange", "mango", "banana"];

let sortedFruits = [...fruits].sort().reverse();

console.log(sortedFruits); // ["orange", "mango", "banana", "apple"]

console.log(fruits); // ["apple", "orange", "mango", "banana"]``````

In the above code, we declared an array of fruits and then created a new array sortedFruits that contains the same elements as the original array fruits. We then used the Array.sort().reverse() method to sort the elements of sortedFruits in descending order and left the original array fruits unchanged.

You can also use a comparator function with the … operator as we did above to sort an array in descending order –

``````// sort array in descending order

let fruits = ["apple", "orange", "mango", "banana"];

let sortedFruits = [...fruits].sort((a, b) => a>b?-1:1);

console.log(sortedFruits); // ["orange", "mango", "banana", "apple"]

console.log(fruits); // ["orange", "mango", "banana", "apple"]``````

In the above code, we declared an array of fruits and then created a new array sortedFruits by using the spread operator. We then used the Array.sort() method with a comparator function to sort the elements of sortedFruits in descending order and left the original array fruits unchanged.

Conclusion

In this article, we saw how to sort an array in descending order in JavaScript using different methods in javascript. We also saw how to use a comparator function with the Array.sort() method to sort the array elements in reverse order.