String.prototype.localeCompare()

번역이 완료되지 않았습니다. Please help translate this article from English

The localeCompare() 메서드는 기준 문자열과 비교했을 때 비교 대상 문자열이 정렬상 전에 오는지, 후에 오는지 혹은 같은 순서에 배치되는지를 알려주는 숫자를 리턴합니다.

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

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

Parameters

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

compareString
The string against which the referring string is compared

Return value

A negative number if the reference string occurs before the compare string; positive if the reference string occurs after the compare string; 0 if they are equivalent.

Description

Returns an integer indicating whether the referenceStr comes before, after or is equivalent to the compareStr.

  • Negative when the referenceStr occurs before compareStr
  • Positive when the referenceStr occurs after compareStr
  • Returns 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.

Examples

Using 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

Sort an array

localeCompare enables a case-insensitive sort of an array.

var items = ['réservé', 'premier', 'cliché', 'communiqué', 'café', 'adieu'];
items.sort((a, b) => a.localeCompare(b)); // ['adieu', 'café', 'cliché', 'communiqué', 'premier', 'réservé']

Check browser support for extended arguments

The 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;
}

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:

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

Using 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

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 (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.2.
ECMAScript 5.1 (ECMA-262)
The definition of 'String.prototype.localeCompare' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'String.prototype.localeCompare' in that specification.
Standard  
ECMAScript (ECMA-262)
The definition of 'String.prototype.localeCompare' in that specification.
Living Standard  
ECMAScript Internationalization API 1.0 (ECMA-402)
The definition of 'String.prototype.localeCompare' in that specification.
Standard locale and option parameter definitions.
ECMAScript Internationalization API 2.0 (ECMA-402)
The definition of 'String.prototype.localeCompare' in that specification.
Standard  
ECMAScript Internationalization API (ECMA-402)
The definition of 'String.prototype.localeCompare' in that specification.
Living Standard  

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
localeCompareChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support 7Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes
localesChrome Full support 24Edge Full support 12Firefox Full support 29IE Full support 11Opera Full support 15Safari Full support 10WebView Android No support NoChrome Android Full support 26Firefox Android No support NoOpera Android No support NoSafari iOS Full support 10Samsung Internet Android Full support 1.5nodejs ?
optionsChrome Full support 24Edge Full support 12Firefox Full support 29IE Full support 11Opera Full support 15Safari Full support 10WebView Android No support NoChrome Android Full support 26Firefox Android No support NoOpera Android No support NoSafari iOS Full support 10Samsung Internet Android Full support 1.5nodejs ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

See also