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 source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
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.
referenceStr.localeCompare(compareString[, locales[, options]])
compareString
referenceStr
is compared.
locales
and
options
These arguments customize the behavior of the function and let applications specify the language whose formatting conventions should be used. In implementations which ignore the
locales
and
options
arguments, the locale used and the form of the string returned are entirely implementation-dependent.
见
Intl.Collator()
构造函数
for details on these parameters and how to use them.
A
negative
number if
referenceStr
occurs before
compareString
;
positive
若
referenceStr
occurs after
compareString
;
0
if they are equivalent.
Returns an integer indicating whether the
referenceStr
comes before, after or is equivalent to the
compareString
.
referenceStr
occurs before
compareString
referenceStr
occurs after
compareString
0
if they are equivalent
Do NOT rely on exact return values of
-1
or
1
!
Negative and positive integer results vary between browsers (as well as between browser versions) because the W3C specification only mandates negative and positive values. Some browsers may return
-2
or
2
, or even some other negative or positive value.
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
特性。
localeCompare()
// The letter "a" is before "c" yielding a negative value
'a'.localeCompare('c'); // -2 or -1 (or some other negative value)
// Alphabetically the word "check" comes after "against" yielding a positive value
'check'.localeCompare('against'); // 2 or 1 (or some other positive value)
// "a" and "a" are equivalent yielding a neutral value of zero
'a'.localeCompare('a'); // 0
localeCompare()
enables case-insensitive sorting for an array.
let items = ['réservé', 'Premier', 'Cliché', 'communiqué', 'café', 'Adieu'];
items.sort( (a, b) => a.localeCompare(b, 'fr', {ignorePunctuation: true}));
// ['Adieu', 'café', 'Cliché', 'communiqué', 'Premier', 'réservé']
locales
and
options
arguments are not supported in all browsers yet.
To check whether an implementation supports them, use the
"i"
argument (a requirement that illegal language tags are rejected) and look for a
RangeError
exception:
function localeCompareSupportsLocales() {
try {
'foo'.localeCompare('bar', 'i');
} catch (e) {
return e.name === 'RangeError';
}
return false;
}
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:
console.log('ä'.localeCompare('z', 'de')); // a negative value: in German, ä sorts before z
console.log('ä'.localeCompare('z', 'sv')); // a positive value: in Swedish, ä sorts after z
options
The results provided by
localeCompare()
can be customized using the
options
argument:
// in German, ä has a as the base letter
console.log('ä'.localeCompare('a', 'de', { sensitivity: 'base' })); // 0
// in Swedish, ä and a are separate base letters
console.log('ä'.localeCompare('a', 'sv', { sensitivity: 'base' })); // a positive value
// by default, "2" > "10" console.log(); // 1 // numeric using options: console.log(); // -1 // numeric using locales tag: console.log(); // -1
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
localeCompare
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5.5 | Opera 7 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
locales
|
Chrome 24 | Edge 12 | Firefox 29 | IE 11 | Opera 15 | Safari 10 | WebView Android No | Chrome Android 26 | Firefox Android No | Opera Android No | Safari iOS 10 | Samsung Internet Android 1.5 |
nodejs
13.0.0
|
options
|
Chrome 24 | Edge 12 | Firefox 29 | IE 11 | Opera 15 | Safari 10 | WebView Android No | Chrome Android 26 | Firefox Android No | Opera Android No | Safari iOS 10 | Samsung Internet Android 1.5 | nodejs 0.12 |
完整支持
不支持
见实现注意事项。
String
String.fromCharCode()
String.fromCodePoint()
String.prototype.anchor()
String.prototype.big()
String.prototype.blink()
String.prototype.bold()
String.prototype.charAt()
String.prototype.charCodeAt()
String.prototype.codePointAt()
String.prototype.concat()
String.prototype.endsWith()
String.prototype.fixed()
String.prototype.fontcolor()
String.prototype.fontsize()
String.prototype.includes()
String.prototype.indexOf()
String.prototype.italics()
String.prototype.lastIndexOf()
String.prototype.link()
String.prototype.localeCompare()
String.prototype.match()
String.prototype.matchAll()
String.prototype.normalize()
String.prototype.padEnd()
String.prototype.padStart()
String.prototype.repeat()
String.prototype.replace()
String.prototype.replaceAll()
String.prototype.search()
String.prototype.slice()
String.prototype.small()
String.prototype.split()
String.prototype.startsWith()
String.prototype.strike()
String.prototype.sub()
String.prototype.substr()
String.prototype.substring()
String.prototype.sup()
String.prototype.toLocaleLowerCase()
String.prototype.toLocaleUpperCase()
String.prototype.toLowerCase()
String.prototype.toSource()
String.prototype.toString()
String.prototype.toUpperCase()
String.prototype.trim()
String.prototype.trimEnd()
String.prototype.trimStart()
String.prototype.valueOf()
String.prototype[@@iterator]()
String.raw()
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()