Comparator

a.k.a. compare function

A function used by a sort to determine the relative order of two values. Returns a negative value if the first argument should be before the second, a positive value if the first argument should be after the second, and 0 if they should be in the same position.


Example

A JavaScript comparator that works for strings and numbers.

function comparator(a, b) {
  if (a < b) return -1;
  if (a === b) return 0;
  return 1;
}