4

when we try to sort an array of Numbers people say use this

var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
   debugger;
   return (a > b) ? (1) : (-1);
});
debugger;
print(numbers);

if we watched the parameter a,b in the debugger mode we see the following

4,2
4,5
1,3
5,1
2,1
2,3
4,3

i need to know or an explanation of how sort function works from inside to give me these parameter ??? or i need some one explain whats happening ?

dersvenhesse
  • 6,276
  • 2
  • 32
  • 53
Marwan
  • 2,362
  • 1
  • 20
  • 35
  • 3
    Why do you need this? The exact sequence of comparator calls is unimportant and unreliable. – user2357112 Feb 06 '14 at 09:24
  • 2
    Personally, I consider this one of the things where I DGAF how it works XD – Niet the Dark Absol Feb 06 '14 at 09:24
  • 4
    check this http://stackoverflow.com/questions/234683/javascript-array-sort-implementation – developerCK Feb 06 '14 at 09:26
  • @NiettheDarkAbsol dont you if you need to if the sort function is incompatible with all browsers ;) you will need to know the prototype to make it to. – Marwan Feb 06 '14 at 09:26
  • 3
    http://www.sorting-algorithms.com/merge-sort – Hless Feb 06 '14 at 09:27
  • @Marwan Erm... .`sort` works in all browsers, and takes a callback which is given `a` and `b` and must return a value depending on how the values compare. That's it. – Niet the Dark Absol Feb 06 '14 at 09:32
  • @developerCK nice one too :) thanks guys – Marwan Feb 06 '14 at 09:32
  • @NiettheDarkAbsol: on top of that, the behaviour of [`Array#sort`](http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.11) is **implementation-defined** (read: unportable and unreliable) unless the sort function exhibits *reflexivity*, *symmetry* and *transitivity*, which the OP's function does not. – DCoder Feb 06 '14 at 09:39
  • On addition to @NiettheDarkAbsol, the callback appears to be called with parameters in random order, but in fact it's part of the algorithm used for the sort function. The return value of the callback tells the algorithm where the element needs to be placed. -1 = a before b, 1 = b before a, atleast in this case. The bigger the array the more the algorithm needs to query certain elements. – Hless Feb 06 '14 at 09:40
  • And one more resource that helped me understand what is actually happening (I'm more a visual guy really) http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Sorting/Gifs/mergeSort.gif – Hless Feb 06 '14 at 09:46
  • Just to put it in, I found a new way for *[find unique and sort](http://stackoverflow.com/questions/21405567/find-unique-and-sort-does-the-latter-only-happen-or-is-it-rule)*, but it is [sloooow](http://jsperf.com/sortvsmyfind). :( – loveNoHate Feb 06 '14 at 09:51
  • because its a merge sort algorithm. – Xline Jan 08 '15 at 22:03

1 Answers1

0

I use this :

var num = [4,2,5,1,3];
num.sort(function(a,b){return a-b;});
ElDoRado1239
  • 3,938
  • 2
  • 16
  • 13
  • its almost the same you just added a case that the result can be zero and in that case no sort will happen and thats not an answer for my question – Marwan Feb 06 '14 at 14:25
  • But it's nicer... :) Ok here's the WebKit's way to do it, if you really care. But there is probably no good reason to study the native code apart from mere curiosity. https://gist.github.com/Pewpewarrows/964673 – ElDoRado1239 Feb 06 '14 at 14:30