4.10 — Sorting & finding
Two jobs remain in every list-worker's toolbelt: putting elements in order, and asking questions about them.
The question-askers (find, some, every, includes) are honest tools. The sorter is a honest tool with the most famous default in JavaScript: call sort() on numbers without telling it how to compare, and it will happily order 120 before 9. Today you see exactly why — and never fall for it again.
const times = [9, 120, 45]; times.sort(); console.log(times); times.sort((a, b) => a - b); console.log(times); const names = ["Mo", "Ada", "Liv"]; console.log(names.find(n => n.length === 3)); console.log(names.some(n => n === "Zed")); console.log(names.every(n => n.length <= 3));
times.sort() — no arguments. The referee needs SOME rule to compare a pair… and the default rule is: convert both to STRINGS, compare alphabetically. Brace yourself.
The deeper story, with the real names for things — this part is what turns “I saw it” into “I can explain it.”
Why is string-sort the default? sort predates most of modern JS and had to work on arrays of anything — strings were the one common denominator. The rule to internalize: sorting numbers always needs a comparator. The comparator contract is just the sign: negative → first argument first, positive → second first, zero → tie.
The scanners short-circuit smartly: find and some stop at the first ✓, every stops at the first ✗ — on a million-element array that matters.
includes uses exact === comparison. That means (4.6 forever) it matches objects only by address: list.includes({ id: 1 }) is false even if a same-shaped object sits right there. For objects, ask some(el => el.id === 1) instead.
Fun fact: your file explorer falls for the same default. Name photos photo1, photo2, … photo10 and watch photo10 sort right after photo1 — alphabetical order on digits, exactly JavaScript's default sort. The workaround humans invented (naming files photo01) is a comparator you apply by hand.
⌨️ leaderboard, highest first
Game night ends and the scores come in unordered. Build the leaderboard — biggest score on top — and run two sanity checks on it.
requirements:
- Start with
scores=[340, 90, 1200, 505]. - Sort it so the HIGHEST score comes first (the lesson sorted ascending — this is the other direction), then print the top score using an index.
- Print whether the board contains a score of exactly
90. - Print whether every score is at least
90.
when you press RUN, the console must show exactly:
✏️ Quick check 1
The classic. Type the FIRST element after this sort:
const a = [100, 25, 3]; a.sort(); console.log(a[0]);
✏️ Quick check 2
Type exactly what this prints:
const a = [3, 10, 1]; a.sort((x, y) => x - y); console.log(a[0]);
✏️ Quick check 3
Type exactly what this prints:
console.log([4, 8, 15].some(n => n > 10));
🗣️ Now teach it back
A friend sorted [9, 30, 200] and got [200, 30, 9]-ish nonsense. Explain what sort does without a comparator, how the comparator’s return value steers the order, and one more surprise sort hides (hint: what happens to the original array?).
Write it as if your friend is sitting next to you. Saved to your journal — future-you will use these notes to teach others.