Revision 386227 of String.prototype.localeCompare()

  • Revision slug: JavaScript/Reference/Global_Objects/String/localeCompare
  • Revision title: String.prototype.localeCompare
  • Revision id: 386227
  • Created:
  • Creator: Norbert
  • Is current revision? No
  • Comment

Revision Content

Summary

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.

Method of String
Implemented in JavaScript 1.2
ECMAScript Edition ECMAScript Language Specification, 3rd Edition
ECMAScript Internationalization API Specification, 1st Edition

Syntax

string.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
{{page('en-US/docs/JavaScript/Reference/Global_Objects/Collator','Parameters')}}

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.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
locales and options arguments 24 {{CompatNightly("Firefox")}} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
locales and options arguments {{ CompatNo() }} 26 {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

See also

Revision Source

<h2 id="Summary">Summary</h2>
<p>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 <code>locales</code> and <code>options</code> 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 <code>locales</code> and <code>options</code> arguments, the locale and sort order used are entirely implementation dependent.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/String" title="JavaScript/Reference/Global_Objects/String"><code>String</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.2</td>
    </tr>
    <tr>
      <td rowspan="2">ECMAScript Edition</td>
      <td>ECMAScript Language Specification, 3<sup>rd</sup> Edition</td>
    </tr>
    <tr>
      <td>ECMAScript Internationalization API Specification, 1<sup>st</sup> Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>string</em>.localeCompare(compareString [, locales [, options]])</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<p>Check the <a href="#Browser_Compatibility" title="#Browser_Compatibility">Browser compatibility</a> section to see which browsers support the <code>locales</code> and <code>options</code> arguments, and the <a href="#Example:_Checking_for_support_for_locales_and_options_arguments">Example: Checking for support for <code>locales</code> and <code>options</code> arguments</a> for feature detection.</p>
<dl>
  <dt>
    <code>compareString</code></dt>
  <dd>
    The string against which the referring string is comparing</dd>
</dl>
<div>
  {{page('en-US/docs/JavaScript/Reference/Global_Objects/Collator','Parameters')}}</div>
<h2 id="Description" name="Description">Description</h2>
<p>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 <code>compareString</code>, returns a positive number if the string occurs afterwards in such a sort, and returns 0 if they occur at the same level.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_localeCompare" name="Example:_Using_localeCompare">Example: Using <code>localeCompare</code></h3>
<p>The following example demonstrates the different potential results for a string occurring before, after, or at the same level as another:</p>
<pre class="brush: js">
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
</pre>
<p>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.</p>
<h3 id="Example:_Checking_for_support_for_locales_and_options_arguments" name="Example:_Checking_for_support_for_locales_and_options_arguments">Example: Checking for support for <code>locales</code> and <code>options</code> arguments</h3>
<p>The <code>locales</code> and <code>options</code> 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 <code>RangeError</code> exception:</p>
<pre class="brush: js">
function localeCompareSupportsLocales() {
    try {
        "a".localeCompare​("b", "i");
    } catch (e) {
        return e​.name === "RangeError";
    }
    return false;
}
</pre>
<h3 id="Example:_Using_locales" name="Example:_Using_locales">Example: Using <code>locales</code></h3>
<p>The results provided by <code>localeCompare</code> 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 <code>locales</code> argument:</p>
<pre class="brush: js">
alert('ä'.localeCompare('z', 'de')); // a negative value: in German, ä sorts with a
alert('ä'.localeCompare('z', 'sv')); // a positive value: in Swedish, ä sorts after z
</pre>
<h3 id="Example:_Using_options" name="Example:_Using_options">Example: Using <code>options</code></h3>
<p>The results provided by <code>localeCompare</code> can be customized using the <code>options</code> argument:</p>
<pre class="brush: js">
// 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
</pre>
<h2 id="Performance">Performance</h2>
<p>When comparing large numbers of strings, such as in sorting large arrays, it is better to create an <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Collator" title="/en-US/docs/JavaScript/Reference/Global_Objects/Collator"><code>Intl.Collator</code></a> object and use the function provided by its <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Collator/compare" title="/en-US/docs/JavaScript/Reference/Global_Objects/Collator/compare"><code>compare</code></a> property.</p>
<h2 id="Browser_Compatibility" name="Browser_Compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari (WebKit)</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
      </tr>
      <tr>
        <td><code>locales</code> and <code>options</code> arguments</td>
        <td>24</td>
        <td>{{CompatNightly("Firefox")}}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Chrome for Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
      </tr>
      <tr>
        <td><code>locales</code> and <code>options</code> arguments</td>
        <td>{{ CompatNo() }}</td>
        <td>26</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Collator" title="JavaScript/Reference/Global_Objects/Collator">Intl.Collator</a></li>
</ul>
Revert to this revision