Sort an Array of Strings in Descending order in TypeScript

avatar

Borislav Hadzhiev

Last updated: Feb 21, 2022

banner

Photo from Unsplash

Sort an Array of strings in Descending order in TypeScript #

To sort an array of strings in descending order:

  1. Call the sort() method passing it a function.
  2. The function will be called with 2 elements from the array.
  3. Return -1 if the first element is greater than the second, otherwise return 1.
index.ts
// ✅ Sort in Descending order const strArray1: string[] = ['a', 'c', 'z', 'f']; const descendingArr = strArray1.sort((a, b) => (a > b ? -1 : 1)); console.log(descendingArr); // 👉️ ['z', 'f', 'c', 'a'] // ✅ Sort in Descending order (Alternative) const strArray3: string[] = ['a', 'c', 'z', 'f']; const descendingArr2 = strArray3.sort().reverse(); console.log(descendingArr2); // 👉️ ['z', 'f', 'c', 'a'] // ✅ Sort in ascending order const strArray2: string[] = ['z', 'c', 'a', 'f']; const ascendingArr = strArray2.sort(); console.log(ascendingArr); // 👉️ ['a', 'c', 'f', 'z']

We used the Array.sort method to sort a string array in a descending order.

Note that the sort() method sorts the elements of the array in place and returns the sorted array. In other words, it mutates the original array.

index.ts
const strArray1: string[] = ['a', 'c', 'z', 'f']; const descendingArr = strArray1.sort((a, b) => (a > b ? -1 : 1)); console.log(descendingArr); // 👉️ ['z', 'f', 'c', 'a'] console.log(strArray1); // 👉️ ['z', 'f', 'c', 'a']

If you want to sort the array without mutating it, use the spread syntax (...) to create a shallow copy before calling the sort() method.

index.ts
// ✅ Sort in Descending order const strArray1: string[] = ['a', 'c', 'z', 'f']; const descendingArr = [...strArray1].sort((a, b) => (a > b ? -1 : 1)); console.log(descendingArr); // 👉️ ['z', 'f', 'c', 'a'] console.log(strArray1); // 👉️ ['a', 'c', 'z', 'f']

We used the spread syntax (...) to unpack the values of the array into a new array before calling the sort method.

This is probably what you want to be doing since mutations can be confusing and difficult to track throughout a codebase.

The sort() and reverse() approach is probably the easiest to read of the 2 approaches in the code snippet.

index.ts
// ✅ Sort in Descending order const strArray3: string[] = ['a', 'c', 'z', 'f']; const descendingArr2 = strArray3.sort().reverse(); console.log(descendingArr2); // 👉️ ['z', 'f', 'c', 'a']

When called without any parameters, the sort method converts the array elements to strings (if necessary) and sorts them according to their UTF-16 code unit values.

The reverse method reverses the array in place and returns the result.

index.ts
// 👇️ ['c', 'b',' 'a'] console.log(['a', 'b', 'c'].reverse());

The parameter we passed to the sort method in the first example is a function that defines the sort order.

index.ts
// ✅ Sort in Descending order const strArray1: string[] = ['a', 'c', 'z', 'f']; const descendingArr = [...strArray1].sort((a, b) => (a > b ? -1 : 1)); console.log(descendingArr); // 👉️ ['z', 'f', 'c', 'a']
If the function parameter is not provided to the sort method, the array elements get converted to strings and sorted according to their UTF-16 code unit values.

This is not what you want when you need to sort an array of strings in descending order, however it's exactly what you want when sorting string arrays in ascending order.

If the return value of the compare function is greater than 0, then sort b before a.

If the return value of the compare function is less than 0, then sort a before b.

If the return value of the compare function is equal to 0, keep the original order of a and b.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.