jsguides

String.prototype.localeCompare()

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

The localeCompare() method compares two strings in the current locale (or a specified locale) and returns a number indicating whether the reference string comes before, after, or is equivalent to the compared string in sort order. This is essential for building multilingual applications that need locale-aware sorting and searching.

Syntax

localeCompare(compareString)
localeCompare(compareString, locales)
localeCompare(compareString, locales, options)

Parameters

ParameterTypeDefaultDescription
compareStringstringThe string to compare against
localesstring or arrayundefinedBCP 47 language tag(s) (e.g., ‘en-US’, ‘de-DE’)
optionsobjectundefinedConfiguration for comparison behavior

Options Object

PropertyTypeDescription
sensitivitystring’base’, ‘accent’, ‘case’, ‘variant’
ignorePunctuationbooleanWhether to ignore punctuation
numericbooleanWhether to use numeric ordering
caseFirststring’upper’ or ‘lower’

Return Value

Returns a negative number if the reference string sorts before compareString, a positive number if it sorts after, or 0 if they are equivalent.

Examples

Basic Comparison

const str = 'apple';

str.localeCompare('banana');   // -1 (apple comes before banana)
str.localeCompare('apple');    // 0 (equal)
str.localeCompare('zebra');    // 1 (apple comes after zebra)

The basic example shows localeCompare() returning the familiar negative, zero, positive pattern for three strings in the default locale. The next block demonstrates passing an explicit locale — 'de-DE' for German phonebook ordering — which changes how accented characters and case variations are handled inside Array.prototype.sort().

Locale-Aware Sorting

const fruits = ['banana', 'Apple', 'cherry'];

// Default comparison
fruits.sort((a, b) => a.localeCompare(b));
// Result: ['Apple', 'banana', 'cherry']

// German phonebook ordering
fruits.sort((a, b) => a.localeCompare(b, 'de-DE'));

Sorting alphabetically by locale is the most common use of localeCompare(), but it also supports numeric collation through the options parameter. The next example sorts version strings in natural numeric order — v2 comes before v10 instead of after — by passing { numeric: true }. This tells the collator to treat embedded numbers as numbers rather than character sequences.

Numeric Sorting

const versions = ['v10', 'v2', 'v1', 'v20'];

// Numeric sorting (v1, v2, v10, v20 instead of v1, v10, v2, v20)
versions.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
// Result: ['v1', 'v2', 'v10', 'v20']

Return value semantics

The specification only guarantees that the return value is negative, zero, or positive — not that it is exactly -1, 0, or 1. Some engines return -1 and 1, others return -2 and 2 or other values. Never compare the result against a specific number other than 0. The correct way to use the return value is as a sort comparator (where any negative means “before”) or to check equality (=== 0).

Sensitivity options explained

The sensitivity option controls what differences are considered significant:

  • "base" — only base letters differ; a and ä are the same, a and b differ
  • "accent" — base letters and accents differ; a and ä differ, but a and A do not
  • "case" — base letters and case differ; a and A differ, but a and ä do not
  • "variant" (default) — all differences matter, including base, accent, case, and other marks
// Case-insensitive comparison
"Apple".localeCompare("apple", undefined, { sensitivity: "base" });
// 0 — treated as equal

// Accent-insensitive
"cafe".localeCompare("café", undefined, { sensitivity: "base" });
// 0 — treated as equal

localeCompare() vs < and >

The plain < and > operators compare strings by Unicode code point values, which produces incorrect results for many languages. For example, sorting German words with < will not respect umlauts. localeCompare() delegates to the Internationalization API (Intl.Collator) and handles language-specific collation rules. For performance-critical sorting of large arrays, create a reusable Intl.Collator instance and call its .compare() method directly — it avoids re-parsing locale options on every call.

Browser Support

Supported in all modern browsers. Internet Explorer requires version 6+. For older browsers, consider using a polyfill.

See Also