Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What really happens in Javascript Sort

I've seen this sort function working fine:

var arr = [1,5,3,7,8,6,4,3,2,3,3,4,5,56,7,8,8];


console.log(arr.sort(
    function(a,b) {
        return a - b;
    }
    ));

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?

like image 682
Adnan Khan Avatar asked Dec 21 '11 11:12

Adnan Khan


People also ask

How does the sort work?

A Sorting Algorithm is used to rearrange a given array or list of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of elements in the respective data structure.

What happens when you sort an array?

sort takes an array and — you guessed it — sorts it in place. No copy of the array is created (as with map , etc.), so the array itself is altered with the sorted values. It can be used with or without a compareFunction , and when one isn't provided, it will automatically sort in ascending order.

How does JavaScript sort work under the hood?

Essentially what's happening under the hood is that the sort function goes through each value inside the array, compares the first value “a” with the second value “b.” If the difference between a and b is a negative number or 0, it does not change the sorting and moves onto the next set.


1 Answers

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.

like image 77
Tikhon Jelvis Avatar answered Oct 03 '22 22:10

Tikhon Jelvis