When it comes to sorting numbers in JavaScript, we can utilize the sort() function with a specific trick that yields perfect results. The tip for successful number sorting is as follows:
[12, 2, 23, 3, 43, 54].sort(function (a, b) { return a - b ; } )
Source #1 and Source#2
The expression a - b
within the sorting function might be perplexing to some, including myself. I attempted to dig into the source code, but found it challenging to comprehend. Although I came across an answer on stackoverflow discussing the Algorithm of JavaScript “sort()” Function, my confusion remains unresolved.
I am eager to learn about what precisely takes place at the core of a - b
. Could someone please shed light on this matter?