Availability
JavaScript 1.5; JScript 5.5; ECMAScript v3
Synopsis
string.localeCompare(target)Arguments
target
A string to be compared, in a locale-sensitive
fashion, with string.
Returns
A number that indicates the result of the comparison. If
string is "less than"
target, localeCompare(
) returns a number less than zero. If
string is "greater than"
target, the method returns a number
greater than zero. And if the strings are identical or
indistinguishable according to the locale ordering conventions, the
method returns 0.
Description
When the < and >
operators are applied to strings, they compare those strings using
only the Unicode encodings of those characters and do not consider
the collation order of the current locale. The ordering produced in
this way is not always correct. Consider Spanish, for example, in
which the letters "ch" are traditionally sorted as if
they were a single letter that appeared between the letters
"c" and "d".
localeCompare( ) provides a way to compare strings
that does take the collation order of the default locale into
account. The ECMAScript standard does not specify how the
locale-specific comparison is done; it merely specifies that this
function utilizes the collation order provided by the underlying
operating system.
Example
You can use code like the following to sort an array of strings into
a locale-specific ordering:
var strings; // The array of strings to sort; initialized elsewhere
strings.sort(function(a,b) { return a.localeCompare(b) });
•
Table of Contents
•
Index
•
Reviews
•
Examples
•
Reader Reviews
•
Errata
JavaScript: The Definitive Guide, 4th Edition
By
David Flanagan
Publisher
: O'Reilly
Pub Date
: November 2001
ISBN
: 0-596-00048-0
Pages
: 936
Slots
: 1
This fourth edition of the definitive reference to
JavaScript, a scripting language that can be embedded
directly in web pages, covers the latest version of the
language, JavaScript 1.5, as supported by Netscape 6 and
Internet Explorer 6. The book also provides complete
coverage of the W3C DOM standard (Level 1 and Level 2),
while retaining material on the legacy Level 0 DOM for
backward compatibility.