Antonio Pavicevac-Ortiz
Antonio Pavicevac-Ortiz

Reputation: 7739

How is this function interacting with the sort method in javaScript?

In a javaScript book I am reading, it sites a handy function which helps the sort method output the values in the correct order (in the case of numbers).

var values = [0,1,5,10,15];
values.sort();
console.log(values); //outputs 0,1,10,15,5

function compare (value1, value2){
    if (value1 < value2){
        return -1;
}    else if (value1 > value2){
        return 1;
}    else {
        return 0;
}

var values = [0,1,5,10,15];
values.sort(compare);
console.log(values); //outputs 0,1,5,10,15

I know what's ultimately happening but I am bit perplexed as how the 'compare' function is doing it! Mostly because I am not sure how the return values of the 'compare' function are effecting the array! Thanks friends!

Upvotes: 0

Views: 49

Answers (2)

jfriend00
jfriend00

Reputation: 707228

When you pass a custom comparison function to the .sort() function, it uses that function to decide which of any two elements in the array belong before or after the other. In more detail, .sort() uses an algorithm to compare all the elements in the array to the other elements, calling your custom function each time it needs to know whether element A or element B of the array belongs before the other. As it collects this information about how the various elements in the array compare to each other, it puts the array into sorted order.

The advantage of doing it this way is that the housekeeping details and algorithm for sorting the array is done entirely internally to the .sort() function and you only have to provide the comparison logic in order to implement a custom sort order. When you decide to return a positive, 0 or negative return value, then that's the comparison function's way of telling the .sort() function which of the two values passed to it belongs earlier in the array and the .sort() function uses that information to place the items in the proper order in the final sorted array. How exactly it does that depends upon which sorting algorithm it is actually using.

If you're curious how .sort() works internally, the details of that are up to a specific javascript implementation, but here's an answer that discusses some of those details and uses terms like mergesort and quicksort which are well known sorting algorithms.

The beauty of javascript's callback design is that you can use .sort() and provide a custom comparison function to create your own sort order without having to know anything about how the inner details of .sort() actually work.

Upvotes: 1

Oriol
Oriol

Reputation: 288050

From MDN article:

If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:

  • If compareFunction(a, b) is less than 0, sort a to a lower index than b, i.e. a comes first.
  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour.
  • If compareFunction(a, b) is greater than 0, sort b to a lower index than a.

Then, if you want to sort numbers, you can use

[0,1,5,10,15].sort(function(a,b) {
    return a-b;
});

Upvotes: 1

Related Questions