mozilla

Revision 237313 of String.prototype.localeCompare()

  • Revision slug: JavaScript/Reference/Global_Objects/String/localeCompare
  • Revision title: localeCompare
  • Revision id: 237313
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment 5 words added, 7 words removed

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.

Method of String
Implemented in JavaScript 1.2
ECMAScript Edition ECMAScript 1st Edition

Syntax

localeCompare(compareString)

Parameters

compareString - The string against which the referring string is comparing

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 -1 if the string occurs earlier in a sort than compareString, returns 1 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('b')); // -1
alert('b'.localeCompare('a')); // 1
alert('b'.localeCompare('b')); // 0

Revision Source

<h3>Summary</h3>
<p>Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/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>ECMAScript Edition</td> <td>ECMAScript 1st Edition</td> </tr> </tbody>
</table> <h3 name="Syntax">Syntax</h3>
<p><code> localeCompare(compareString) </code></p>
<h3 name="Parameters">Parameters</h3>
<p><code>compareString</code> - The string against which the referring string is comparing</p>
<h3 name="Description">Description</h3>
<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 -1 if the string occurs earlier in a sort than <code>compareString</code>, returns 1 if the string occurs afterwards in such a sort, and returns 0 if they occur at the same level.</p>
<h3 name="Examples">Examples</h3>
<h4 name="Example:_Using_toUpperCase">Example: Using <code>localeCompare</code></h4>
<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('b')); // -1
alert('b'.localeCompare('a')); // 1
alert('b'.localeCompare('b')); // 0</pre>
Revert to this revision