re: Sorting Arrays of Strings in JavaScript VIEW POST

FULL DISCUSSION
 

There's a big caveat to this method of string sorting: it doesn't take into account Unicode and non-English alphabets into account. Depending on your application, you might want to consider using the String::localeCompare() instead which has built-in support for things like language-specific sort ordering, ignoring cases or diacritics:

const strings = ['č','é','A','b','Đ'];

const defaultSort = Array.from(strings).sort();

const simpleSort = Array.from(strings).sort((a, b) => a - b);

const localeSort = Array.from(strings).sort((a, b) => {
  return a.localCompare(b, 'en', { sensitivity: 'base' });
});

console.log(defaultSort);
console.log(simpleSort);
console.log(localeSort);
code of conduct - report abuse