public
Created

trying to wrap my head around sorting

  • Download Gist
sort
1 2 3 4 5 6 7 8 9 10 11
var myArray = [10, 44, 32, 100, 0, 44, 3, 4];
console.log(myArray.toString()); // 10, 44, 32, 100, 0, 44, 3, 4
 
myArray.sort();
console.log(myArray.toString()); // 0,10,100,3,32,4,44,44 --> sorted like strings
 
myArray.sort(function (a, b) {
return a - b;
console.log(a,b);
});
console.log(myArray.toString()); // 0,3,4,10,32,44,44,100 --> sorted numerically

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.