localeCompare
The localeCompare task compares two strings in the current locale and returns a numeric value (negative, zero, or positive) indicating whether the reference string comes before, after, or is the same as the compareString in sort order. The locale is based on the language settings of the browser.
Potential use case
Use this task to sort an array of strings with special characters, or to implement “natural sorting” in which numbers embedded in strings are treated numerically (sorted low to high). The task also provides multiple options to control how certain formatting conventions are handled when sorting.
Properties
Example 1
In this example, referenceStr is "Hello World" and compareString is "héllö wôrld" (with an acute accent on é, a diaeresis on ö, and a circumflex on ô).

The compareResult is -1 (negative) because the reference string comes before the comparison string.

Example 2
In this example, referenceStr is "8" and compareString is "30".

The compareResult is 1 (positive) because the string value "8" comes after "30" in string sort order.

Example 3
In this example, referenceStr is "réservé" (with acute accents) and compareString is "reserve" (no accents). The locales option is set to "en" (English), and options includes "sensitivity": "base" to indicate the strings do not have the same base letters.

The compareResult is 0 because the two strings are considered equal under these settings.

Related reading
See the Intl.Collator constructor on MDN for more information on the locales and options parameters.