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
?