JavaScript - Sort an Array in Reverse Order in JS Last Updated : 15 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report JS array.sort() Method sorts the array in ascending order. We can make it descending or reverse order using array.reverse() method. Below are the following ways to sort an array in reverse order:1. Using array.sort() with array.reverse()First, sort the given array of strings using JS array.sort() method. JavaScript const a = [ "JS", "CSS", "HTML" ] a.sort(); console.log(a) Output[ 'CSS', 'HTML', 'JS' ] Now, use the array.reverse() method to reverse the array and get sort the array in descending order. JavaScript const a = [ "JS", "CSS", "HTML" ] // Sort in ascending order a.sort(); // Reverse the order a.reverse() console.log(a) Output[ 'JS', 'HTML', 'CSS' ] 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.2. Using the Comparator FunctionFor Numeric ArrayUse the comparision function to subtract first item from second to get the reverse order. JavaScript const arr = [ 10, 20, 25, 100 , 40] // Pass the comparator to to sort in reverse order arr.sort((a,b) => b - a) console.log(arr) 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.For Array of StringsUse string.localeCompare() to compare first element relative to second element. JavaScript // Original array let arr = ["JS", "HTML", "CSS" ]; console.log(arr); // Sort in reverse order arr.sort( (a,b) => b.localeCompare(a)) console.log(arr); Go through this article, sort array of strings in JS, to learn more about sorting strings Array in JavaScript.3. Using Lodash.sortBy() MethodLodash _.sortBy() method sorts the array in ascending order. We can get the reverse array by passing additional parameter. JavaScript const _ = require('lodash'); const arr = [10, 20, 25, 100, 40]; // Sort in descending with a custom iteratee _.sortBy(arr, (n) => -n); console.log(arr); Output[100, 40, 25, 20, 10]4. Using Lodash _.reverse()MethodLodash _.reverse() Method reverses and updates the original array. Use it on the sorted array to get the descending order. JavaScript const _ = require('lodash'); const arr = [10, 20, 25, 100, 40]; // Sort the array arr.sort((a, b) => a - b); // Then reverse the orignal sorted array _.reverse(arr); console.log(arr); Output[100, 40, 25, 20, 10] Comment More infoAdvertise with us Next Article JavaScript Array reverse() Method S shobhit_sharma Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-Questions JavaScript-Array-Questions +1 More Similar Reads Reverse an Array in JavaScript Here are the different methods to reverse an array in JavaScript1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array.JavaScriptlet a = [1, 2, 3 3 min read Sort an Array in JavaScript This article will show you how to sort a JS array.1. Using array.sort() Method To sort an array in JavaScript, we can use array.sort() method. This method sorts the elements alphabetically in ascending order.JavaScriptlet arr = ["JS", "HTML", "CSS"]; arr.sort() console.log(arr);Output[ 'CSS', 'HTML' 3 min read Sort An Array Of Arrays In JavaScript The following approaches can be used to sort array of arrays in JavaScript.1. Using array.sort() Method- Mostly UsedJS array.sort() is used to sort and update the original array in ascending order.JavaScriptlet arr = [[3, 2], [1, 4], [2, 5], [2, 0]]; arr.sort(); console.log(arr);Output[ [ 1, 4 ], [ 2 min read Sort an array of objects using Boolean property in JavaScript Given the JavaScript array containing Boolean values. The task is to sort the array on the basis of Boolean value with the help of JavaScript. There are two approaches that are discussed below: Table of Content Using Array.sort() Method and === OperatorUsing Array.sort() and reverse() MethodsUsing a 2 min read JavaScript Array reverse() Method The JavaScript Array reverse() method reverses the order of the elements in an array in place. The first array element becomes the last, and the last element becomes the first, and so on.It modifies the original array and returns a reference to the reversed array. We can also use the Array.toReverse 3 min read Sort Mixed Alpha/Numeric array in JavaScript Sorting a mixed alpha/numeric array in JavaScript involves arranging the elements in a specified order, typically lexicographically (dictionary order) for strings and numerically for numbers. When these types are mixed, a custom sort function is required to effectively handle comparisons between dif 3 min read Like