The problem with the default alphabetical sort
Problem
The encoding for Ó
is way after ASCII characters so it puts Ólafur
at the end
['Ólafur', 'A', 'N', 'O', 'P', 'Z'].toSorted()
// [ 'A', 'N', 'O', 'P', 'Z', 'Ólafur' ]
Solution: Use Intl.Collator
const collator = new Intl.Collator("en", { sensitivity: "base" });
['Ólafur', 'A', 'N', 'O', 'P', 'Z'].toSorted((a, b) => collator.compare(a, b));
// ['A', 'N', 'O', 'Ólafur', 'P', 'Z']
Learned from the tip today!