beginner35 minby DevFox

Sorting and its pitfalls

Put a list in order with sort, and avoid its three traps: with no rule it compares everything as text, so 10 comes before 5 and capitals jump ahead; it changes the list itself; and its rule must answer with a number. Write a compare function, (a, b) => a - b, sort words with localeCompare, keep the old order with toSorted, and make TaskKit print its list from A to Z.

  • TypeScript
  • Fundamentals
Sorting and its pitfalls. Read this lesson at .