MDN may have intermittent access issues April 18 13:00 - April 19 01:00 UTC. See whistlepig.mozilla.org for all notifications.

mozilla
Your Search Results

    String.prototype.localeCompare Redirect 1

    Summary

    The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

    The new locales and options arguments let applications specify the language whose sort order should be used and customize the behavior of the function. In older implementations, which ignore the locales and options arguments, the locale and sort order used are entirely implementation dependent.

    Syntax

    str.localeCompare(compareString [, locales [, options]])

    Parameters

    Check the Browser compatibility section to see which browsers support the locales and options arguments, and the Example: Checking for support for locales and options arguments for feature detection.

    compareString
    The string against which the referring string is comparing
    locales

    A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the locales argument, see the Intl page. The following Unicode extension keys are allowed:

    co
    Variant collations for certain locales. Possible values include: "big5han", "dict", "direct", "ducet", "gb2312", "phonebk", "phonetic", "pinyin", "reformed", "searchjl", "stroke", "trad", "unihan". The "standard" and "search" values are ignored; they are replaced by the options property usage (see below).
    kn
    Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are "true" and "false". This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence.
    kf
    Whether upper case or lower case should sort first. Possible values are "upper", "lower", or "false" (use the locale's default). This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence.
    options

    An object with some or all of the following properties:

    localeMatcher
    The locale matching algorithm to use. Possible values are "lookup" and "best fit"; the default is "best fit". For information about this option, see the Intl page.
    usage
    Whether the comparison is for sorting or for searching for matching strings. Possible values are "sort" and "search"; the default is "sort".
    sensitivity
    Which differences in the strings should lead to non-zero result values. Possible values are:
    • "base": Only strings that differ in base letters compare as unequal. Examples: a ≠ b, a = á, a = A.
    • "accent": Only strings that differ in base letters or accents and other diacritic marks compare as unequal. Examples: a ≠ b, a ≠ á, a = A.
    • "case": Only strings that differ in base letters or case compare as unequal. Examples: a ≠ b, a = á, a ≠ A.
    • "variant": Strings that differ in base letters, accents and other diacritic marks, or case compare as unequal. Other differences may also be taken into consideration. Examples: a ≠ b, a ≠ á, a ≠ A.

    The default is "variant" for usage "sort"; it's locale dependent for usage "search".

    ignore­Punctua­tion
    Whether punctuation should be ignored. Possible values are true and false; the default is false.
    numeric
    Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are true and false; the default is false. This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence. Implementations are not required to support this property.
    caseFirst
    Whether upper case or lower case should sort first. Possible values are "upper", "lower", or "false" (use the locale's default); the default is "false". This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence. Implementations are not required to support this property.

    Description

    Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order. Returns a negative number if the string occurs earlier in a sort than compareString, returns a positive number if the string occurs afterwards in such a sort, and returns 0 if they occur at the same level.

    Examples

    Example: Using localeCompare

    The following example demonstrates the different potential results for a string occurring before, after, or at the same level as another:

    alert('a'.localeCompare('c')); // -2, or -1, or some other negative value
    alert('c'.localeCompare('a')); // 2, or 1, or some other positive value
    alert('a'.localeCompare('a')); // 0
    

    Note that the results shown in the code above can vary between browsers and browser versions. This is because the values are implementation-specific. That is, the specification requires only that the before and after values are negative and positive.

    Example: Checking for support for locales and options arguments

    The locales and options arguments are not supported in all browsers yet. To check whether an implementation supports them already, you can use the requirement that illegal language tags are rejected with a RangeError exception:

    function localeCompareSupportsLocales() {
        try {
            "a".localeCompare​("b", "i");
        } catch (e) {
            return e​.name === "RangeError";
        }
        return false;
    }
    

    Example: Using locales

    The results provided by localeCompare vary between languages. In order to get the sort order of the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the locales argument:

    alert('ä'.localeCompare('z', 'de')); // a negative value: in German, ä sorts with a
    alert('ä'.localeCompare('z', 'sv')); // a positive value: in Swedish, ä sorts after z
    

    Example: Using options

    The results provided by localeCompare can be customized using the options argument:

    // in German, ä has a as the base letter
    alert('ä'.localeCompare('a', 'de', {sensitivity: "base"})); // 0
    
    // in Swedish, ä and a are separate base letters
    alert('ä'.localeCompare('a', 'sv', {sensitivity: "base"})); // a positive value
    

    Performance

    When comparing large numbers of strings, such as in sorting large arrays, it is better to create an Intl.Collator object and use the function provided by its compare property.

    Specifications

    Specification Status Comment
    ECMAScript 3rd Edition. Standard Initial definition.
    Implemented in JavaScript 1.2
    ECMAScript Language Specification 5.1th Edition (ECMA-262) Standard  
    ECMAScript Language Specification 6th Edition (ECMA-262) Draft  
    ECMAScript Internationalization API Specification, 1st Edition Standard locale and option parameter defintions

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
    locales and options arguments 24 29 (29) 11 15 Not supported
    Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)
    locales and options arguments Not supported 26 Not supported

    bug 864843

    Not supported Not supported Not supported

    See also

    Document Tags and Contributors

    Contributors to this page: Sheppy
    Last updated by: Sheppy,
    Hide Sidebar