JavaScript · Senior · Question 71 of 75

How do you sort names correctly for a given language?

const collator = new Intl.Collator("uk", { sensitivity: "base" });
names.sort(collator.compare);

Explanation

Default sort compares UTF-16 code units, so "Ä", "ї" or uppercase letters end up in wrong places. numeric: true also sorts "item2" before "item10".

Further reading

Problem with a question

JavaScript · Senior · question 71

Test yourself

Take the JavaScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz