Open In App

Sort an array in reverse order JavaScript

Last Updated : 13 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sorting is a basic algorithm that is used in many programming languages. Most languages provide in-built methods to sort an array. In JavaScript we use the sort() method to sort an array in ascending order. To sort an array in the reverse order that is descending order there are no built-in methods to perform this task but we can combine different methods together to sort the array in reverse order.

These are the following methods for solving this problem:

Approach 1: Using sort() method and reverse() method

  • Initialize an array with some values.
  • Sort the array using the sort() method 
  • Reverse the array using the reverse() method.

Example: This example implements the above approach.

Javascript




const arr1 = [24.6,23.7,18.9,76.5];
const arr2 = [54,23,12,97,100];
 
function arrSort(arr) {
    arr.sort((a,b)=>a-b);
    arr.reverse();
    return arr;
}
 
console.log(arrSort(arr1));
console.log(arrSort(arr2));


Output

[ 76.5, 24.6, 23.7, 18.9 ]
[ 100, 97, 54, 23, 12 ]

Approach 2: Using sort() method and comparator function

  • Initialize an array with some values.
  • Sort the array with the modified sort() method.
  • Pass a callback function as a parameter inside the sort method.
  • It will be a comparator function that will sort in descending order.

Example: This example implements the above approach.

Javascript




const arr1 = [24.6,23.7,18.9,76.5];
const arr2 = [54,23,12,97,100];
 
function arrSort(arr) {
    arr.sort((a,b)=>b-a);
    return arr;
}
 
console.log(arrSort(arr1));
console.log(arrSort(arr2));


Output

[ 76.5, 24.6, 23.7, 18.9 ]
[ 100, 97, 54, 23, 12 ]

Approach 3: Using sort() method and Lodash _.reverse() function

  • In this appraoch, we are using Lodash _.reverse() method for reversing the given array.
  • We first sort our array then we will reverse it by using _.reverse() method and print the result in the console.

Example: This example implements the above approach.

Javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let array1 = [1, 7, 3, 4]
 
// Printing original Array
console.log("original Array1: ", array1)
 
array1.sort();
// Using _.reverse() method
let reversedArray = _.reverse(array1);
 
// Printing the reversedArray
console.log("reversed Array: ", reversedArray)


Output:

original Array1:  [ 1, 7, 3, 4 ]
reversed Array: [ 7, 4, 3, 1 ]

Conclusion:

There is no predefined method to sort an array in reverse order but we can create additional methods to sort in descending order. We can combine some array methods to reverse in descending order or we can create a custom comparator function to sort in descending order.
We have have an article Reverse an Array in JavaScript by using some other approaches as well please go through this article to clear your concept.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads