Locale compare just means words will be compared in dictionary order, alphabetically, using the locale specified (which is by default whichever one is set on the system you are using). Rather than by character code.
So at a basic level, it just allows a sort
function to sort alphabetically, and do so correctly regardless of language.
In many cases, there will be a text file on your computer somewhere which is just a list of rules and exceptions for your locale. The system (and in turn a program that requires the information from it, ie in this case a browser) will load this file. In turn, the information held within it will then be made available to things like the browser’s i18n API (of which localeCompare is part of).
For more specific examples:
Take the locale I use most often, “en-GB”, because I live in the UK. The modern English alphabet has 26 letters. But some words may use accented characters, for example naïve or café
Say I have a program that accepts user input, I want to allow someone to type either cafe or café, or naive or naïve.
If I then want to sort those words that the user typed, then I want the program to treat them the same, hence I either write the logic myself or I just use localeCompare
.
Similarly, there are a number of words in English which use letters that are not in the standard alphabet. For example, the word mediæval. “æ” is an actual letter, it’s not a ligature. It’s just that it’s mainly been replaced by the digraph “ae”. Again, in a program, I would want to treat mediaeval and mediæval as the same thing.
Similarly, there are UK names and placenames use letters that are not in the standard modern alphabet. Irish Gaelic (18 letter alphabet) makes heavy use of acute accents on certain characters, and very occasionally a dot to indicate the séimethe (afaik usage of the dot has almost completely disappeared tho).
Béal Feirste
Scottish Gaelic alphabet (23 letters) includes grave-accented versions of vowels.
Abhainn a’ Ghlinne Mhòir
Welsh uses a circumflex to indicate lengthened characters (and also uses both acute and grave accents).
Caersŵs
If I have a list of placenames, I likely want those in the correct alphabetical order, regardless of whether their spelling uses letters that do not exist in the standard alphabet.