2

i was wondering how javascript Array sort method works, when a custom sorting function is specified like the one below :

arr = [1, 5, 122, 12];
arr.sort(function (a,b){return a-b;});

if it only accepts two arguments a,b (1,5) in "arr", i know that javascript allow more arguments than those specified in the function. but how sort function is acting, does it compares 1,5 then 122,12, store result in a place then redo the comparison.

  • There are many different way to sort an array like Quicksort, Mergesort and so on. You just need to tell the `sort` function how to compare two elements. – fafl Feb 07 '17 at 11:40
  • See this http://stackoverflow.com/questions/1494713/how-does-javascripts-sort-work – Nagarjuna Reddy Feb 07 '17 at 11:41
  • thanks alvaro, my question has a full focus on the sort algorithm. – Marouane El-anbri Feb 07 '17 at 11:46
  • To emphasize what @ÁlvaroGonzález said, you are providing *only* a compare function, the browsers may use different algorithms to do the sort. I don't know about how the other browsers implement it, but I do know that Chrome actually uses 2 different algorithms depending on the length of the array. For shorter arrays (10 or less IIRC) it uses quicksort; it uses a different (and likely more efficient) algorithm for longer arrays. – Useless Code Feb 07 '17 at 11:49
  • thanks fafl, useless Code, now i'm getting the whole picture – Marouane El-anbri Feb 07 '17 at 11:55

2 Answers2

2

when a custom sorting function is specified

This is not correct. You provide a function to compare items:

arr.sort()
arr.sort(compareFunction)

compareFunction

Specifies a function that defines the sort order. If omitted, the array is sorted according to each character's Unicode code point value, according to the string conversion of each element.

The sort algorithm itself is hard-coded in native implementation and cannot be changed. Implementations may even choose to pick different algorithms depending on data types or array length.

The rationale is that you may have different needs:

  • You may want to use binary collation and make A different from a or you may want to use Spanish collation and make a identical to á.

  • You may want to sort custom objects:

    [
        {
            city: "Madrid",
            population: 4000000
        },
        {
            city: "Cairo",
            pages: 15000000
        }
    ]
    
  • Or you may want to sort fruits and make pear come before apple :)

However, the sort algorithm itself (quicksort, etc.) is an implementation detail that normally doesn't matter for your business logic.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
0

It is well explained in the MDN documentation:

  • 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.
  • If compareFunction(a, b) is greater than 0, sort b to a lower index than a.

So there is nothing to do with having more than two arguments.

If you are asking in which order it compares them, it is up to implementation. You can find it doing a console.log in the function:

arr = [1, 5, 122, 12];
arr.sort(function (a,b){console.log('Comparing a:', a, 'and b:', b); return a-b;});
rpadovani
  • 7,101
  • 2
  • 31
  • 50
  • i know that, i'm just wondering how the sort function its self acts, when it pass the third argument. – Marouane El-anbri Feb 07 '17 at 11:44
  • @MarouaneEl-anbri a third argument isn't supplied to the function. In the specification there is explicitly written `If comparefn is not undefined, it should be a function that accepts two arguments x and y` – rpadovani Feb 07 '17 at 11:49