I can't seem to figure this one out.
var arr = [2.62, 111.05, 1.05]
arr.sort();
This returns [1.05, 111.05, 2.62], but I'm expecting [1.05, 2.62, 111.05].
How can this be achieved? I've read a bit about writing a custom sort to split the "decimal" but haven't managed to have any success.
By default sorting is alphabetically. You need to pass a function to the sort
method
arr.sort(function(a, b){return a-b;});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments