Nerd For Tech
Published in

Nerd For Tech

Basics of Javascript · String · localeCompare() (method)

This article is a transcript of my free youtube series about basics of web development. If you prefer watching over reading, feel free to visit my channel “Dev Newbs”.

Hello dev newbs! The method that we will cover today is of special importance to me and probably to all of you that come from the country that uses special characters in their alphabet. By the way, I am from Slovakia and we have a lot of diacritic marks in our words and names. So if you are like me and you need to order words, names, or whatever made of strings, this is the method for you.

LocaleCompare() method returns a number which indicates whether a reference string belongs alphabetically before, or after, or is the same as the given string in sort order. This method compares two strings in the current locale. The locale is based on the language settings of the browser.

The parameters of the method are as follows:

  • compared string — the string used to compare against referenced string
  • locales — value to specify what language to use for comparison
  • options — object containing arguments that allow to further specify the behaviour of the method

The locales & options might not be implemented in the same manner in all browsers. If they are not implemented at all, they will be ignored. There is a way of specifying the same attributes both in locales and options, but there are three reasons why I won’t teach you how to do it within locale setup. First is already mentioned — it is not implemented in each browser. Second is that I don’t like it because it’s very clumsy. The third is that you can override these settings in the options object anyway.

The number that you get as a result is one of these three options:

  • negative number — if the reference string is alphabetically before compared string
  • 0 if two strings are equal
  • positive number if the reference string is alphabetically after compared string

Because W3C specification does not specify using exact numbers, only the positivity and negativity of the result number, some browsers can provide 1/-1, others can provide 2/-2 or even completely different values. So only check whether the value returned is more or less or equal to zero.

Ok, so much for the definition. Let’s see the method in action.

I included a comment at the beginning of the example to show the sort order for the most common characters — that is numbers, then alphabet letters with leading lowercase letter followed by uppercase letter in the alphabetical order. The four example results confirm this basic ordering.

Now, we talked about diacritic marks and the fact that some languages have different variations of the basic letters. For example both Germans and Swedes know the letter “ä”. But there is a difference in the way how their locale handles this letter. We will have a look at this in the second example:

First pair of results shows us that for Germans, “ä” comes before the letter “z”, but for the Swedes, they put their characters with diacritic marks after the base set of letters of the alphabet.

Similar situation is in the second pair of results which illustrates the difference between two locales regarding the “baseness” of the letter. For Germans, “ä” is just the base letter “a” with an extra diacritic mark. They do not consider this a separate letter. On the other hand, Swedes consider it a separate letter.

Okay, so that’s that. If you thought that locales are bad, prepare for a treat. We are going to cover options now.

Now, there are more attributes available for the options object, but I will cover only those that worked for me and actually made sense to use. Feel free to check the rest.

First and probably most often used one is attribute “sensitivity”. You can set it to one of the following four values:

  • base
  • accent
  • case
  • variant (default)

When we use sensitivity option three checks are done on each compared pair of letters:

  • whether two base letters should be considered equal
  • whether base letter and version with diacritic mark should be considered equal
  • whether uppercase and lowercase version of letter should be considered equal

Each of the four available sensitivity options sets a different combination of these checks. You can see it in the following example.

You can check the combination setup for each sensitivity value in the comment line. The default one if no sensitivity is provided, is “variant”, which is the most sensitive one.

The last example will show two other attributes and that is “numeric” which specifies whether to compare two strings as if they represented a number. The default setting is false.

Interesting is that even if you define numeric attribute with value false, Chrome still behaves as if you chose “true”. In a way, it makes sense, because what would be the reason to specify numeric as false, when it is false by default? But I think that still — it’s a bit of a lazy approach to implementation.

The other attribute I want to mention is “caseFirst” which decides which of the lowercase/uppercase version of a letter comes first.

Its values can be:

  • upper — uppercase is first
  • lower — lowercase is first
  • false (default) — use the locale’s default sorting

Okay, enough was said, here are the examples:

Just bear in mind that if you run the examples in the different browser or the same browser, but a very different version, the results might differ. Also, if your default locale is not the same as mine, the results might differ a bit.

That’s that for the day. I know it was a lot, but if you made it here, enjoy. It is over and you did well.

As always thank you for the attention and I will see you in the next article.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jakub Korch

Web enthusiast, programmer, husband and a father. Wannabe entrepreneur. You name it.