JavaScript sorts numbers as text.
transcript
You sort four numbers in JavaScript. You pass no comparator. The sort runs, and the answer is wrong. The result looks odd.
The numbers are ten, nine, one, one hundred. You call sort with nothing inside. Nothing looks wrong in the code. What comes back?
It returned one, ten, one hundred, nine. Nine came last. Nine is smaller than ten, but not as text. That is text order.
Without a comparator, items become text. The text ten starts with the character one. Letters and digits compare character by character. So ten sorts before nine.
Pass a comparator that subtracts. It returns a number. The comparator subtracts the second from the first. Never return a boolean.
With the comparator, the order is right. One, nine, ten, one hundred. It is also short, so there is little to remember. Always pass a comparator for numbers.
Always pass a comparator. Full breakdown in the lecture.