Exercise 9 · Chapter: ArraysOptional+50 XP
Fix the Sort
This code sorts an array of numbers — but plain sort() compares them as text, so 10 comes before 2 (because "1" comes before "2" in the alphabet).
Fix the code so the numbers sort correctly from smallest to biggest.
Expected output:
2 5 10 30
Hint: Give sort() the number-compare rule: (a, b) => a - b.
Need a hint?
3 available · costs 5 XP each
javascript
let numbers = [10, 2, 30, 5];
numbers.sort();
for (let n of numbers) {
console.log(n);
}
Output
Run your code to see the output here.