jQuery - Sort array in ascending order -
i need sort nrarray
array:
var nrarray = nrarray.sort();
what above this:
["1", "17", "206", "22", "3", "6"]
i need this:
["1", "3", "6", "17", "22", "206"]
pass in comparison callback , use parseint
like
var arr = ["1", "17", "206", "22", "3", "6"]; arr.sort(function(a, b){ return parseint(a)- parseint(b); }); console.log(arr);
update
you dont need parseint
a/b
auto-converted numbers. because subtracting , javascript performs necessary type conversion. however, same cannot said a + b
string concatenation.
Comments
Post a Comment