I have what may seem to be a basic question about the .sort
method in Javascript.
I just need a little more "down-to-earth" explanation about how this works:
If I have an array:
var myArray = [40, 50, 30, 70];
I want to sort myArray
from smallest to largest, I would do:
myArray.sort(function(a,b) {
return a-b;
});
So, stepping through that...
- 40 - 50 = -10 ... so since the value
-10
is less than0
.. keep as is. - 50 - 30 = 20 ... so since the value
20
is greater than0
.. rearrange array to:
[40,30,50,70]
.
So, once the rearrangement is done.. I assume does the sort
method start over?
Sorry, if this is super basic, but just curious.. maybe I'm missing something that happens.