0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Sort Array in Descending Order

Use array.sort((a, b) => b - a) to sort a JavaScript array in descending order.
📋

Examples

Input[3, 1, 4, 2]
Output[4, 3, 2, 1]
Input[10, 20, 5, 15]
Output[20, 15, 10, 5]
Input[]
Output[]
🧠

How to Think About It

To sort an array in descending order, think about comparing two numbers at a time and placing the larger number before the smaller one. We use a comparison function that subtracts the first number from the second to decide their order.
📐

Algorithm

1
Get the input array.
2
Use a sorting method with a comparison function that compares two elements.
3
In the comparison, subtract the first element from the second to order from largest to smallest.
4
Return the sorted array.
💻

Code

javascript
const arr = [3, 1, 4, 2];
arr.sort((a, b) => b - a);
console.log(arr);
Output
[4, 3, 2, 1]
🔍

Dry Run

Let's trace sorting [3, 1, 4, 2] in descending order using the comparison function.

1

Initial array

[3, 1, 4, 2]

2

Compare 3 and 1

b - a = 1 - 3 = -2 (negative), so 3 stays before 1

3

Compare 3 and 4

b - a = 4 - 3 = 1 (positive), so 4 moves before 3

4

Compare 1 and 2

b - a = 2 - 1 = 1 (positive), so 2 moves before 1

5

Final sorted array

[4, 3, 2, 1]

ComparisonResultAction
3 vs 11 - 3 = -23 before 1
3 vs 44 - 3 = 14 before 3
1 vs 22 - 1 = 12 before 1
💡

Why This Works

Step 1: Sorting with a comparison function

The sort method can take a function that compares two elements to decide their order.

Step 2: Descending order logic

By subtracting b - a, the function returns a positive number if b is larger, so it places b before a.

Step 3: In-place sorting

The sort method changes the original array to the sorted order.

🔄

Alternative Approaches

Using reverse after ascending sort
javascript
const arr = [3, 1, 4, 2];
arr.sort((a, b) => a - b);
arr.reverse();
console.log(arr);
Sorts ascending first, then reverses; less efficient but easy to understand.
Using a custom bubble sort
javascript
const arr = [3, 1, 4, 2];
for(let i = 0; i < arr.length; i++) {
  for(let j = 0; j < arr.length - i - 1; j++) {
    if(arr[j] < arr[j + 1]) {
      [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
    }
  }
}
console.log(arr);
Manual sorting method; educational but slower for large arrays.

Complexity: O(n log n) time, O(1) space

Time Complexity

The built-in sort method uses an efficient sorting algorithm like Timsort or QuickSort, which runs in O(n log n) time on average.

Space Complexity

Sorting is done in-place, so extra space is minimal, O(1).

Which Approach is Fastest?

Using sort((a, b) => b - a) is fastest and simplest; reversing after sorting adds extra steps; manual sorting is slow and mainly for learning.

ApproachTimeSpaceBest For
sort((a, b) => b - a)O(n log n)O(1)Fast and simple for all arrays
sort ascending + reverseO(n log n)O(1)Simple but slightly less efficient
Manual bubble sortO(n²)O(1)Learning and very small arrays
💡
Always provide a comparison function to sort when sorting numbers to avoid incorrect order.
⚠️
Using sort() without a comparison function sorts numbers as strings, causing wrong order.