Algorithms and Data Structures · Junior · Question 34 of 75
What does this code return?
[10, 1, 5, 100].sort();- 1
[100, 10, 5, 1] - 2
[1, 5, 10, 100] - ✓Correct answer:
[1, 10, 100, 5]
Explanation
Without a comparator sort converts elements to strings and compares them as text, so "10" and "100" come before "5". For numbers pass a comparator: sort((a, b) => a - b).