0
var num_list = [1, 2, 3, 4];
function num_order(a, b) {return b-a; }
num_list.sort(num_order);

I've been through blogs and i have searched on this topic but to no avail. All describe this function to be sorting in descending order but none specify how does that happen.. For example, what values are stored in the parameters a and b and how are these values assigned.. Finally what results the function passes and how does sort method do to those values.. Any help would be appreciated..

Let me specify that there is a post similar to this but the answer in that post is not clear.. One of the user has provided a link here which makes it much clearer

2

2 Answers 2

2

The parameter you pass to the sort method is the comparison function. It will define the order the elements are sorted.

To see what values are being passed to the parameters a and b. Try this in your console:

var num_list = [1, 2, 3, 4];

num_list.sort(function(a, b) {
    debugger;
    return b-a;
})

MDN has good documentation on the compare function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

1
  • Excellent.. I understood the logic.. This is the actual answer.. I have voted up!! Commented Jan 11, 2014 at 7:08
-3

Have a look at any sort algorithm : at some point they need to compare two elements of the array.
The sort function of most Browsers is heapsort or quicksort, but i will take bubble sort as an example of a sort algorithm :

 n = array size
 repeat
    swapped = false 
    for i= 0 to n-2
        if array [i] > array [i+1] then 
           swap ( array [i] , array [i+1] )
           swapped = true
    end for
    n = n -1
 until swapped = false

we can easily rewrite the comparison to use a comparison function :

 n = array size
 repeat
    swapped = false 
    for i= 0 to n-2
        a = array [i]
        b = array [i+1]
        if compareFunction(a,b) > 0  then 
           swap ( array [i] , array [i+1] )
           swapped = true
    end for
    n = n -1
 until swapped = false

with :

 compareFunction (a,b) 
    return a-b

So the comparison function is just a function that returns an integer that reflect the items order.
If two elements are the same => the function returns 0, first is bigger => returns >0, second is bigger => returns <0.

Using a comparison function allows to sort any kind of array (i.e. an array containing any kind of item), while still using the very same sort algorithm as the one used for integer sort.

1
  • I think OP is asking about the mechanics of how Array.sort uses a comparison function and hooks everything up, rather than why the comparison function logically produces descending order. Commented Jan 11, 2014 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.