Unexpected output using Intl.Collator with Latvian alphabet

151 Views Asked by At

I need to sort an array of objects by key in Latvian language but when I use Intl.Collator or compareLocale, the output is not correct.

My example:

function letterSort(lang, strings) {
  strings.sort(new Intl.Collator(lang).compare);
  return strings;
}
console.log(letterSort('lv', ['ā', 'a', 'ābols','anna']));
// expected output: Array ["a", "anna", "ā", "ābols"]
// actual output: Array ["a", "ā", "ābols", "anna"]

Any ideas what can be done? Thank you!

1

There are 1 best solutions below

0
Gaccoo On

It might be old, but I was also working with Latvian and had the same problem. I got it working like this.

const comparer = new Intl.Collator(undeifned, {numeric: true,sensitivity: "variant",}).compare;

function sortByNameAscending(list: List[]): List[] {
  return sort(list).by([{ asc: (u) => u.name[0], comparer }]);
}

I had more parameters to sort by, but this is a stripped version of it. You just use the first letter rather than the whole word.

Edit: It will sort the first letters correctly, but it will fall back in the next. (Ceļ, Cel)