How does sort function work in JavaScript, along with compare function

The “compare” function must take two arguments, often referred to as a and b. Then you make the compare function return 0, greater than 0, or less than 0, based on these values, a and b.

  1. Return greater than 0 if a is greater than b
  2. Return 0 if a equals b
  3. Return less than 0 if a is less than b

With these three return values, and only two arguments, it is possible to write a compare function that can sort any type of input data type, or complex data structures.

Then, when you call sort(), with your custom compare function, the compare function is called on pairs in your to-be-sorted list, to determine the proper ordering.

Lets walk through a simple example… Suppose you’re only sorting some numbers, so we have a very simple compare function:

function compare(a,b) {
    return a - b;
}

Simply subtracting b from a will always return greater than zero if a is larger than b, 0 if they are equal, or less than zero if a is less than b. So it meets the requirements for a compare function.

Now lets suppose this is our list of numbers to sort:

var numbers = [1,5,3.14];

When you call numbers.sort(compare), internally it will actually execute:

compare(1,5);     // Returns -4, a is less than b
compare(1,3.14);  // Return -2.14, a is less than b
compare(5,3.14);  // returns 1.86, a is greater than b

If you’ve ever done manual sorting or alphabetizing, you’ve done precisely the same thing, probably without realizing it. Even though you may have dozens or hundreds of items to compare, you’re constantly comparing only two numbers (or author’s last names, or whatever) at a time. Going through or short list of three numbers again, you’d start by comparing the first two numbers:

  1. Is 1 greater than or less than 5? Less than, so put these two numbers in our list: 1,5
  2. Is 3.14 greater than or less than 1? Greater than, so it goes after 1 in the new list
  3. Is 3.14 greater than or less than 5 in our new list? Less than, so it goes before 5. Our new list is now [1,3.14,5]

Because you can provide your own compare() function, it is possible to sort arbitrarily complex data, not just numbers.

Leave a Comment