Skip to content

.tip

· one of the small things

Intl.Collator sorts names the way a reader expects

.sort() with no comparator compares UTF-16 code units, so Ömer comes after Zeynep and every lowercase name after every uppercase one. Nobody reading that list thinks it is sorted.

const byName = new Intl.Collator("tr", { numeric: true });
const sorted = people.toSorted((a, b) => byName.compare(a.name, b.name));

numeric: true also puts bolum9 before bolum10. Build the collator once and keep it, construction costs far more than a comparison does.

javascripti18n