var set = [3,100, 70, 55, 47, 202];
var sortedNumbers = set.sort();
console.log(sortedNumbers)
I see the following in the console:
[100, 202, 3, 47, 55, 70]
Why isn't the array sorted?
var set = [3,100, 70, 55, 47, 202];
var sortedNumbers = set.sort();
console.log(sortedNumbers)
I see the following in the console:
[100, 202, 3, 47, 55, 70]
Why isn't the array sorted?
The sort
function treats the array values as strings. Instead you could use:
function sortNumber(a, b) {
return a - b;
}
var sortedNumbers = set.sort(sortNumber);