Skip to content

Comparators

Eugene Lazutkin edited this page Jul 19, 2026 · 4 revisions

This module is a collection of helpers to adapt different styles of comparators: compare(a, b) (used in Array.prototype.sort()) and less(a, b).

The classic definition of compare(a, b):

  • If a < b, it returns a negative value.
  • If a > b, it returns a positive value.
  • If a == b, it returns 0.

The classic definition of less(a, b):

  • If a < b, it returns a truthy value.
  • Otherwise, it returns a falsy value.

Both styles are comparable and can be converted to each other. The main difference is the possible complexity of the implementation.

Examples of compare and less:

const compareNumbers = (a, b) => a - b;
const lessNumbers = (a, b) => a < b;

const compareStrings = (a, b) => a.localeCompare(b);
const lessStrings = (a, b) => a < b;

When designing a comparator, it is important to understand the following:

  • What information is required? If "less" is enough, use less(). If you need an equality as well, use compare(). To wit:

    const search1 = (root, value, compare) => {
      if (!root) return null;
      const result = compare(value, root.value);
      if (result < 0) return search1(root.left, value, compare);
      if (result > 0) return search1(root.right, value, compare);
      return root;
    };
    
    const search2 = (root, value, less) => {
      if (!root) return null;
      if (less(value, root.value)) return search2(root.left, value, less);
      if (less(root.value, value)) return search2(root.right, value, less);
      return root;
    };

    As you can see in the code above, search1 and search2 are very similar. The only difference is that search1 requires compare() and search2 requires less(), which can be called twice.

  • Possible complexity? To wit:

    const compareNumbers1 = (a, b) => a - b;
    const compareNumbers2 = (a, b) => (a < b ? -1 : a > b ? 1 : 0);

    Clearly the first one is easier to read and likely to be faster than the second one.

comparators.js

Legend for tables
  • API
    • compareFn — a compare function (see above).
    • lessFn — a less function (see above).
    • equalFn — an equal function. It returns a truthy value if a and b are equal and a falsy value otherwise.

The following utilities are available:

Function Return value Description
compareFromLess(lessFn) compareFn Creates a compare function from a less function.
lessFromCompare(compareFn) lessFn Creates a less function from a compare function.
equalFromLess(lessFn) equalFn Creates an equal function from a less function.
equalFromCompare(compareFn) equalFn Creates an equal function from a compare function.
reverseCompare(compareFn) compareFn Creates a compare function that reverses its arguments.
reverseLess(lessFn) lessFn Creates a less function that reverses its arguments.

reverseCompare() and reverseLess() reverse the arguments:

  • reverseCompare(compareFn)(a, b) is an equivalent to -compareFn(a, b).

  • reverseLess(lessFn)(a, b) is not an equivalent to !lessFn(a, b). Example:

    const less = (a, b) => a < b;
    const negatedLess = (a, b) => a >= b;
    const reversedLess = (a, b) => a > b;

Exports

All functions are exported by their names. There is no default export.

Clone this wiki locally