Sort an Array of Strings in Descending order
To sort an array of strings in descending order:
- Declare and Initialize an Array.
- Apply the
sort()
array method to sort the elements. - Apply the
reverse()
method on prototype chain. - The array will be sorted in reverse order.
// Sort in Descending order
const initArray = ['a', 'c', 'z', 'f'];
const descArray = initArray.sort().reverse();
console.log(descArray);
// Alternative approach to Sort in Descending order
const descArrayAlt = initArray.sort((a, b) => (a > b ? -1 : 1));
console.log(descArrayAlt); // 👉️ ['z', 'f', 'c', 'a']
// Sort in ascending order
const ascArr = initArray.sort();
console.log(ascArr);
In the example, We used the Array.sort method to sort a array in descending order.
The sort() method sorts the elements of the array and returns the sorted array. This method mutates the original array.
example –
const theArray = ['a', 'c', 'z', 'f'];
const descArray = theArray.sort().reverse();
console.log(descArray);
console.log(theArray); // Mutated the original array.
Sort an array of strings in descending order using the spread syntax (…).
If you want to sort the array without mutating it, you can use the spread syntax (…) to create a shallow copy before calling the sort()
method.
// Sort in Descending order
const theArray = ['a', 'c', 'z', 'f'];
const descArray = [...theArray].sort().reverse();
console.log(descArray);
console.log(theArray);
// Sort in Descending order (Alternative)
const theArrayTwo = ['a', 'c', 'z', 'f'];
const descArrayTwo = [...theArrayTwo].sort((a, b) => (a > b ? -1 : 1));
console.log(descArrayTwo);
We used the spread syntax (…) to unpack the values of the array into a new array. We did this so that we could call the sort method on the new array.
ℹ️ This is the most likely thing you want to do, since mutations can be confusing and difficult to track throughout a large codebase.
The sort()
and reverse()
approach is probably the easiest way to read the code snippet.
// Sort the array in Descending order
const theArray = ['a', 'c', 'z', 'f'];
const descArray = theArray.sort().reverse();
console.log(descArray);
The sort() method will convert the array elements to strings and sort them according to their UTF-16 code unit values if no parameters are given.
ℹ️ The reverse method will reverse the array in place and return the result.
console.log(['a', 'b', 'c'].reverse());
The sort method in the second example takes a function as a parameter that defines the sort order.
const theArrayTwo = ['a', 'c', 'z', 'f'];
const descArrayTwo = [...theArrayTwo].sort((a, b) => (a > b ? -1 : 1));
console.log(descArrayTwo);
If no function parameter is provided to the sort method, the array elements are converted to strings and sorted in ascending order according to their UTF-16 code unit values.
The function is used to determine the order of two elements in an array. If the return value of the function is greater than 0
, then element b
is sorted before element a
. If the return value of the compare function is less than 0
, then element a is sorted before element b. If the return value of the function is equal to 0
, then the original order of the two elements is maintained.