← Path
JavaScript Fundamentals · JavaScript Objects, Arrays and Array Methods (map, filter, reduce)
0 XP- 1 current stop: learning
- 2 upcoming
- 3 upcoming
- 4 upcoming
- 5 upcoming
Finish after stop 5
Stop 1 of 5 · Warehouse aisle: sorting the shelf
Trick to keep in mind+3 XP
Bare sort() sorts as text: compare by letters, not by size
With no comparator, sort turns items into strings and orders them character by character, like a dictionary. Under that rule "100" sorts before "25" because "1" comes before "2". For real numeric order pass (a, b) => a - b.
console.log([25, 3, 100].sort());
console.log([25, 3, 100].sort((a, b) => a - b));Read it once, then a question tests whether the trick stuck.