For sorting numbers in javascript we trick function sort() given in Javascript and it works perfectly. The trick is given below:
[12, 2, 23, 3, 43, 54].sort(function (a, b) { return a - b ; } )
Source #1 and Source#2
I didn't understand what exactly this a - b does. I have checked source code but its hard to understand. I checked following answer from stackoverflow but my doubt haven't cleared yet.
Algorithm of JavaScript “sort()” Function
Can anyone explain me what exactly happens at a - b?
Array#sortsortproperty takes as arguments a pair of 2 elements from the array and you define the way they are to be compared, in this case you subtract one from the other to decide which one is biggerfunction (a, b) { return a - b ; }.... so it doesnt actually do anything its more a statement than an instruction