JavaScript localeCompare() method


The localeCompare() method in JavaScript is used to compare two strings in the current locale. It can determine the relative order of the strings, which is useful for sorting strings in a way that takes into account the linguistic conventions of a specific locale.

Syntax:

string.localeCompare(compareString, locales, options)
  • compareString: The string to compare the original string against.
  • locales (optional): A string with a locale code (e.g., "en", "fr", "de") or an array of locale codes. This specifies the locale to use for the comparison.
  • options (optional): An object that can specify various options for the comparison, such as sensitivity (case sensitivity), ignore punctuation, etc.

Return Value:

  • The method returns a number:
    • A negative number if the original string comes before the compareString in sort order.
    • 0 if the two strings are considered equal.
    • A positive number if the original string comes after the compareString in sort order.

Example 1: Basic Usage

let str1 = "apple"; let str2 = "banana"; let result = str1.localeCompare(str2); console.log(result); // -1 (apple comes before banana)

Example 2: Case Sensitivity

The comparison is case-sensitive by default, meaning uppercase letters are sorted before lowercase letters.

let str1 = "apple"; let str2 = "Apple"; console.log(str1.localeCompare(str2)); // 1 (lowercase 'a' comes after uppercase 'A')

Example 3: Using Locales

You can specify locales to influence the sorting behavior based on specific language conventions.

let str1 = "äpfel"; // "apple" in German let str2 = "apfel"; console.log(str1.localeCompare(str2, 'de')); // -1 (the German locale places "ä" before "a")

Example 4: Options for Customizing Behavior

You can provide options to modify the comparison behavior, such as case sensitivity and ignoring diacritics.

let str1 = "resume"; let str2 = "résumé"; console.log(str1.localeCompare(str2, 'en', { sensitivity: 'base' })); // 0 (ignores accents, considered equal) console.log(str1.localeCompare(str2, 'en', { sensitivity: 'variant' })); // 1 (takes accents into account)

Example 5: Sorting an Array of Strings

You can use localeCompare() to sort an array of strings in a locale-aware manner.

let fruits = ["banana", "Apple", "cherry", "apricot"]; fruits.sort((a, b) => a.localeCompare(b)); console.log(fruits); // ["Apple", "apricot", "banana", "cherry"]

Example 6: Comparing with Different Locales

You can compare strings in different languages by specifying appropriate locale codes.

let str1 = "café"; let str2 = "cafe"; console.log(str1.localeCompare(str2, 'fr', { sensitivity: 'base' })); // 0 (in French, they are considered equal) console.log(str1.localeCompare(str2, 'en', { sensitivity: 'base' })); // 1 (in English, 'é' is not equal to 'e')

Summary:

  • The localeCompare() method is used to compare two strings based on locale-specific rules.
  • It returns a number indicating the order of the strings: negative if the original string comes first, 0 if equal, and positive if the original string comes second.
  • The method supports optional parameters to specify the locale and options for customizing the comparison behavior, such as case sensitivity and ignoring accents.
  • It is particularly useful for sorting strings in a way that respects the linguistic and cultural conventions of different languages.