Underscore.js _.comparator() Method Last Updated : 11 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report The _.comparator() method takes a binary predicate-like function and returns a comparator function which can be used as a callback for the _.sort() method etc. Syntax: _.comparator( function ); Parameters: function: a predicate-like function defined. Return Value: This method returns a comparator function. Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed. underscore.js contrib library can be installed using npm install underscore-contrib –save. Example 1: Sorting using a comparator function. javascript // Defining underscore contrib variable const _ = require('underscore-contrib'); let gfgFun = function (x, y) { // Returns -1, 0 or 1 return x <= y; }; // Array let arr = [4, 8, 2, 9, 1]; let comp = _.comparator(gfgFun); // Using comparator function with _.sort() method arr.sort(comp); console.log("Sorted Array :", arr) Output: Sorted Array : [ 1, 2, 4, 8, 9 ] Example 2: Reverse Sorting using a comparator function. javascript // Defining underscore contrib variable const _ = require('underscore-contrib'); let gfgFun = function (x, y) { // Returns -1, 0 or 1 return x >= y; }; // Array let arr = [4, 8, 2, 9, 1]; let comp = _.comparator(gfgFun); // Using comparator function with _.sort() method arr.sort(comp); console.log("Sorted Array :", arr) Output: Sorted Array : [ 9, 8, 4, 2, 1 ] Comment More infoAdvertise with us Next Article Underscore.js _.comparator() Method T taran910 Follow Improve Article Tags : JavaScript Web Technologies Node.js JavaScript - Underscore.js Similar Reads Underscore.js _.gt() Method The _.gt() Method checks whether each argument is greater than its next argument. Syntax: _.gt( val1, val2,..., valn ); Parameters: This method takes n values to operate on them. Return Value: This method returns a Boolean value. Note: This will not work in normal JavaScript because it requires the 1 min read Underscore.js _.gte() Method The _.gte() Method checks whether each argument is greater than or equal to its next argument. Syntax: _.gte( val1, val2,..., valn ); Parameters: This method takes n values to operate on them. Return Value: This method returns a Boolean value. Note: This will not work in normal JavaScript because it 2 min read Underscore.js _.best() Method The _.best() method takes an array and a function and generates the best suitable value from that array using the conditions of the function. Syntax: _.best(array, function)Parameters:Â This method accepts two parameters as mentioned above and described below: array: The given array from which the be 2 min read Underscore.js _.eq() Method The _.eq() method compares the arguments with loose equality (==). _.eq( val1, val2,..., valn ); Parameters: This method takes n values to operate on them. Return Value: This method returns a boolean (true if arguments are equal else false). Note: This will not work in normal JavaScript because it r 2 min read Underscore.js _.lt() Method The _.lt() Method checks whether each argument is lesser than its next argument. Syntax: _.lt( val1, val2, ..., valn ); Parameters: This method takes n values to operate on them. Return Value: This method returns a Boolean value. Note: This will not work in normal JavaScript because it requires the 2 min read Like