3

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!

Kristine
  • 737
  • 2
  • 9
  • 22
  • Intl sometimes behaves differently based on browser or OS locale settings, so while the sorting might not work correctly on a system set to en-US, it might when switched to lv-LV. – idmadj Aug 26 '19 at 00:14
  • I can’t speak about the Latvian language specifically, but in the localized collations I’m aware of, diacritical marks only affect sorting order when comparing to the same letter with a different mark (or no mark at all) *in the same location*. You can think of it as if everything was first sorted with all diacriticals removed, and then the order is refined only where the unaccented forms would be identical. – kshetline Aug 26 '19 at 00:16
  • I just did some experimentation with different locales, and always got the same sort order, even if I used an invalid locale like "xx". It might be that the JavaScript you're using simply doesn't have support for Latvian, and you're getting generic Unicode sorting instead. There doesn't seem to be a JavaScript `Intl` method that will tell you what locales are actually supported, unfortunately. – kshetline Aug 26 '19 at 00:28
  • here some explanation for this problem could help: http://www.indiscripts.com/post/2010/10/alphabetical-sort-in-javascript-and-indesign – Nemer Aug 26 '19 at 00:46

0 Answers0