JavaScript sort trick
Sorting strings in JavaScript
The JavaScript sort method is very useful when we want to sort numbers, or even strings. But when sorting strings, we ought to be careful because the strings might be in different languages. Luckily, there’s a fix for that. It’s called localeCompare:
referenceString.localeCompare(compareString)
The referenceString
is the string we want to compare, and the compareString
is the string against which we’re compareing. The localeCompare
method returns a Number, which may be:
- -1 if the
referenceString
is sorted before thecompareString
- 0 if the two strings are equal
- 1 if the
referenceString
is sorted after thecompareString
Here’s an example:
1
2
3
4
5
6
7
8
9
10
11
const words = ['hello', 'répondre', 'sort', 'déclaré'];
words.sort((a, b) => a.localeCompare(b));
As we can see, some words have certain punctuations. Using localeCompre
with sort helps us sort the English and French words in our array successfully. More documentaion can be found here