But I don't really understand the mechanics of this little function. When it is comparing a and b, which numbers of the array is it really comparing? If say, it picked up the first two numbers 1 and 5, the function will return -4. What does that mean to the sort order? Or is it just the negative boolean value? Even if it is, how does the sort really happen?

3 Answers
3

Basically, the sort works by comparing two elements at a time. A comparison is more than a boolean--you have three options: less than, equal and greater than. In JavaScript, these three values are represented by n < 0, 0 and n > 0 respectively.

In other words, negative numbers mean a < b; 0 means a = b and positive means a > b.

To answer the broader question: there are some relatively fast algorithms for sorting a list by comparing its elements. The most popular is Quicksort; however, Quicksort is not stable so some engines (Firefox's for sure) use a different algorithm. A simple stable sort is Mergesort.

Sorting algorithms are often some of the first algorithms analyzed in intro CS classes because they are simple but still interesting and nontrivial enough to illustrate how to analyze algorithms in general. You should read about them for this reason, and simply because they're pretty cool.

Slightly random aside:

You could also imagine using a special type (like an enum) for this sort of thing. The comparing function could return LT, GT or EQ as appropriate, for example. However, in a dynamic language like JavaScript, it's much easier just to use numbers. In languages more obsessed with types (like Haskell :)), using a special order type makes more sense.

Okay, gone through the Mergesort Wikipedia entry and it makes sense (broadly). But if I wanted to take a look at how the sort function implementation has been written in Javascript, how can I?
–
AdnanDec 21 '11 at 11:42

@Capex - Javascript doesn't use any one sort algorithm, that is, there's no specification that says "must use bubble sort" or whatever. Different Javascript implementations (in different browsers) may use different sort algorithms. But the array sort method with the a and b parameters will still be the same to Javascript programmers because it simply provides a way for the underlying algorithm to compare any two given values.
–
nnnnnnDec 21 '11 at 11:55

1

I think the issue here is that the built-in sort function is probably not written in JavaScript--I suspect most engines implement it in native code for performance reasons. You can see webkit's sort here if you scroll down a bit. If you just want any sorting algorithm in JavaScript, here's a literate one that has plenty of commentary.
–
Tikhon JelvisDec 21 '11 at 11:55

When passing a function to Array.sort, it will be used to compare a and b. How they will be sorted depends on what the function returns:

If result < 0, then a comes before b;

If result == 0, then a and b are already in the right order;

If result > 0, then a comes after b.

If sort is called without an argument, the items are converted to strings and compared alfabetically.

By the way, which algorithm sort uses depends entirely on the browser's implementation. It could be quick sort, merge sort, or anything else. The ECMAScript standard doesn't specify any requirements in this regard.