Skip to main content

How to Sort an Array of Strings in Descending Order in JavaScript

Sorting an array of strings is a fundamental operation in JavaScript. While the default Array.prototype.sort() method sorts strings in ascending (A-Z) alphabetical order, you often need to sort in descending (Z-A) order.

This guide will teach you the modern, standard, and most robust method for sorting strings in descending order using the localeCompare() method. You will also learn the critical best practice of sorting immutably to avoid bugs.

The Core Method: sort() with a Compare Function

The Array.prototype.sort() method is the built-in tool for sorting arrays. While its default behavior works for simple, ascending string sorts, you must provide a compare function for any other type of sorting, including descending order.

A compare function takes two arguments (let's call them a and b) from the array and must return:

  • A negative number if a should come before b.
  • A positive number if b should come before a.
  • 0 if their order should not change.

The Solution: localeCompare() for Robust String Sorting

The String.prototype.localeCompare() method is the recommended best practice for comparing any two strings. It is language-sensitive and correctly handles special characters, accents, and different alphabets.

To sort in descending order, you simply reverse the comparison by calling b.localeCompare(a).

Problem: you have an array of strings and want to sort them from Z to A.

// Problem: How to sort this array in descending order?
let fruits = ['banana', 'apple', 'cherry', 'date'];

Solution:

let fruits = ['banana', 'apple', 'cherry', 'date'];

// Use a compare function with localeCompare for a descending sort.
fruits.sort((a, b) => {
return b.localeCompare(a);
});

console.log(fruits); // Output: ['date', 'cherry', 'banana', 'apple']
note

This is the most robust and internationally-friendly way to sort strings in JavaScript.

A Best Practice: Sorting Immutably

A critical characteristic of the sort() method is that it mutates (modifies) the original array in place. This can lead to bugs in larger applications where the original order might be needed elsewhere.

The best practice is to create a copy of the array before sorting it. The modern spread syntax (...) makes this easy.

let originalFruits = ['banana', 'apple', 'cherry', 'date'];

// 1. Create a shallow copy of the array with the spread syntax.
// 2. Sort the new array.
let sortedFruits = [...originalFruits].sort((a, b) => b.localeCompare(a));

console.log(sortedFruits); // Output: ['date', 'cherry', 'banana', 'apple']
console.log(originalFruits); // Output: ['banana', 'apple', 'cherry', 'date'] (The original is unchanged)

Output:

['date', 'cherry', 'banana', 'apple']
['banana', 'apple', 'cherry', 'date']
note

This immutable approach leads to safer and more predictable code.

A Simpler (but Less Robust) Alternative

For simple, English-only strings without special characters, you can use comparison operators (<, >) to achieve a descending sort.

let fruits = ['banana', 'apple', 'cherry'];

fruits.sort((a, b) => {
if (a > b) {
return -1; // a should come after b, so we return a negative value
}
if (a < b) {
return 1; // a should come before b, so we return a positive value
}
return 0;
});

console.log(fruits); // Output: ['cherry', 'banana', 'apple']
note

While this works, localeCompare() is generally preferred because it correctly handles a much wider range of strings and is often more readable.

Conclusion

Sorting an array of strings in descending order is a simple task that requires a custom compare function.

  • The recommended best practice is to use the localeCompare() method in reverse order: array.sort((a, b) => b.localeCompare(a)).
  • For safe and predictable code, always sort immutably by creating a copy of the array first: let sorted = [...myArray].sort(...).
  • Avoid the default sort().reverse() pattern, as it is less direct and can be less performant than using a proper compare function from the start.