Reverse order comparators in JavaScript
This post will discuss how to create and use reverse order comparators in JavaScript.
A reverse order comparator is a function that compares two values and returns a negative, zero, or positive number depending on whether the first value is smaller, equal, or larger than the second value, but in the opposite order of the natural or default ordering. For example, if the natural ordering of numbers is ascending (from smallest to largest), then a reverse order comparator would return a negative number if the first number is larger than the second number, a zero if they are equal, and a positive number if the first number is smaller than the second number. This way, the reverse order comparator can be used to sort an array of numbers in descending order (from largest to smallest). There are several ways to create and use reverse order comparators in JavaScript:
1. Using Array.sort() function with a custom comparator function
This function allows us to sort an array of values by passing a custom comparator function as an argument to the Array.sort() function. The comparator function takes two values as parameters and returns a negative, zero, or positive number based on their comparison. To create a reverse order comparator, we can either swap the order of the parameters in the comparison expression, or negate the result of the comparison expression. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Create an array of numbers const arr = [1, 2, 3, 4, 5]; // Define a reverse order comparator function by swapping the parameters function compareReverse(a, b) { // Compare b with a instead of a with b return b - a; } // Sort the array using the reverse order comparator arr.sort(compareReverse); // Log the sorted array console.log(arr); // [5, 4, 3, 2, 1] |
2. Using a higher-order function that returns a reverse order comparator
This function allows us to create a reusable function that takes another comparator function as an argument and returns a new comparator function that reverses its order. We can use this function to create reverse order comparators for different types of values or criteria. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// Define a higher-order function that returns a reverse order comparator function reverseComparator(comparator) { return function(a, b) { // Negate the result of the original comparator return -comparator(a, b); }; } // Create an array of strings const arr = ["apple", "banana", "cherry", "date", "elderberry"]; // Define a natural order comparator function for strings function compareStrings(a, b) { // Compare strings using locale-specific rules return a.localeCompare(b); } // Create a reverse order comparator for strings using the higher-order function const compareStringsReverse = reverseComparator(compareStrings); // Sort the array using the reverse order comparator for strings arr.sort(compareStringsReverse); // Log the sorted array // ["elderberry", "date", "cherry", "banana", "apple"] console.log(arr); |
That’s all about creating and using reverse order comparators in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)