The following program (taken from a tutorial) prints the numbers in an array in order from lowest to highest. In this case, the result will be 2,4,5,13,31
My question relates to the paramaters "a" and "b" for the function pareNumbers. When the function is called in numArray.sort(pareNumbers)
what numbers will be the parameters a and b for the function. Does it just move along the array. For example, start with a=13
and b=2
? After that, does the function run again paring a=2 and b=31? or would it next pare a=31
and b=4
?
Can someone please explain how that part works and also how it manages to sort them from lowest to highest? I don`t see how the function manages to do the necessary calculations on the numbers in the array.
function pareNumbers(a,b) {
return a - b;
}
var numArray = [13,2,31,4,5];
alert(numArray.sort(pareNumbers));
The following program (taken from a tutorial) prints the numbers in an array in order from lowest to highest. In this case, the result will be 2,4,5,13,31
My question relates to the paramaters "a" and "b" for the function pareNumbers. When the function is called in numArray.sort(pareNumbers)
what numbers will be the parameters a and b for the function. Does it just move along the array. For example, start with a=13
and b=2
? After that, does the function run again paring a=2 and b=31? or would it next pare a=31
and b=4
?
Can someone please explain how that part works and also how it manages to sort them from lowest to highest? I don`t see how the function manages to do the necessary calculations on the numbers in the array.
function pareNumbers(a,b) {
return a - b;
}
var numArray = [13,2,31,4,5];
alert(numArray.sort(pareNumbers));
Share
Improve this question
edited Apr 12, 2012 at 16:54
templatetypedef
374k111 gold badges944 silver badges1.1k bronze badges
asked Mar 2, 2011 at 1:29
mjmitchemjmitche
2,0676 gold badges25 silver badges31 bronze badges
4 Answers
Reset to default 8The particular pairs that get passed in depend on the sorting algorithm being used. As the algorithm tries to go about sorting the range, it needs to be able to pare pairs of values to determine their ordering. Whenever this happens, it will call your function to get that parison.
Because of this, without inside knowledge about how the sorting algorithm works, you cannot predict what pairs will get pared. The choice of algorithm will directly determine what elements get pared and in what order.
Interestingly, though, you can actually use the parison function to visualize how the sort works or to reverse-engineer the sorting algorithm! The website sortviz has many visualizations of sorting algorithms generated by passing custom parators into sorting functions that track the positions of each element. If you take a look, you can see how differently each algorithm moves its elements around.
Even more interestingly, you can use parison functions as offensive weapons! Some sorting algorithms, namely quicksort, have particular inputs that can cause them to run much more slowly than usual. In "A Killer Adversary for Quicksort," the author details how to use a custom parator to deliberate construct a bad input for a sorting algorithm.
Hope this helps!
The two parameters will be elements of your array. The system will pare enough pairs to be able to sort them correctly. Nothing else is guaranteed.
There are lots of things the sort method could be doing under the hood; see, e.g., http://en.wikipedia/wiki/Sorting_algorithm for some of them. Most Javascript implementations probably use some variant of either quicksort or mergesort.
(Here are super-brief descriptions of those. Quicksort is: pick an element in the array, rearrange the array to put everything smaller than that in front of everything larger, then sort the "smaller" and "larger" bits. Mergesort is: sort the first half of the array, sort the second half of the array, and then merge the two sorted halves. In both cases you need to sort smaller arrays, which you do with the same algorithm until you get to arrays so small that sorting them is trivial. In both cases, good practical implementations do all sorts of clever things I haven't mentioned.)
It will be called for all pairs of a,b that sorting algorithm need to get all array sorted. Check out http://en.wikipedia/wiki/Sorting_algorithm for brief list of sorting algorithms.
When you pass a function to Array.sort()
, it expects two parameters and returns a numerical value.
If you return a negative value, the first parameter will be placed before the second parameter in the array.
If you return a positive value, the first parameter will be placed after the second parameter in the array.
If you return 0, they will stay in their current position.
By doing return a - b;
, you are returning a negative number if a is less than b (2 - 13 = -11)
, a positive number if b is less than a (13 - 2 = 11)
, and zero if they are even (13 - 13 = 0)
.
As far as which numbers are pared in what order, I believe that is up to the javascript engine to determine.
Check out the documentation on javascript array sorting at the MDC Doc Center for more detailed information. https://developer.mozilla/en/JavaScript/Reference/Global_Objects/Array/sort
(BTW, I always check the MDC Doc Center for any questions about how javascript works, they have the best information on the language AFAIK.)