JavaScriptStrings

String.prototype.localeCompare()

Compares two strings according to language rules. Returns a negative number, zero, or a positive number, perfect for sorting.

Returns
A negative number when the string should come before compareString, a positive number when after it, and 0 when they are equivalent.
Changes the original
No
On this page

Example

#
JavaScript
const students = ['Özlem', 'Zoe', 'mia', 'Marco', 'Eva']

console.log([...students].sort())
console.log([...students].sort((a, b) => a.localeCompare(b, 'de')))
Console
['Eva', 'Marco', 'Zoe', 'mia', 'Özlem']
['Eva', 'Marco', 'mia', 'Özlem', 'Zoe']

Definition and usage

#

The localeCompare() method checks whether, in alphabetical order, a string should come before another string, after it, or whether they are equivalent. It returns a negative number, zero, or a positive number, exactly what the comparator function in sort() expects.

Plain sort() without a comparator orders text by character code, so accented letters end up at the end, and uppercase letters come before lowercase ones. With localeCompare() and a language code such as "de", the letter Ö sits right after O, just like in a dictionary.

The third argument is an options object. sensitivity: "base" treats a and A as equal, and numeric: true compares numbers found in text by value, so "lesson2" sorts before "lesson10". For sorting large lists, Intl.Collator with the same options is faster.

Syntax

#
Syntax
string.localeCompare(compareString)
string.localeCompare(compareString, locales)
string.localeCompare(compareString, locales, options)

Parameters

#
  • compareString

    string

    The string to compare against.
  • locales

    string | string[], optional

    A language code, such as "en" or "de", that determines the ordering rules. Defaults to the browser's language.
  • options

    object, optional

    Comparison settings, including sensitivity ("base", "accent", "case", "variant") and numeric (comparing numbers by value).

More examples

#
The returned value
JavaScript
console.log('a'.localeCompare('b', 'de'))
console.log('b'.localeCompare('a', 'de'))
console.log('cat'.localeCompare('cat', 'de'))
console.log('ö'.localeCompare('p', 'de') < 0)
Console
-1
1
0
true
A case-insensitive comparison
JavaScript
const answer = 'germany'
const correct = 'Germany'

console.log(answer === correct)
console.log(answer.localeCompare(correct, 'de', { sensitivity: 'base' }) === 0)
Console
false
true
Numbers in names
JavaScript
const lessons = ['lesson10', 'lesson2', 'lesson1']

console.log([...lessons].sort())
console.log([...lessons].sort((a, b) => a.localeCompare(b, 'de', { numeric: true })))
Console
['lesson1', 'lesson10', 'lesson2']
['lesson1', 'lesson2', 'lesson10']

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Check only the sign of the result (< 0, > 0, === 0), not a specific number. The specification does not guarantee it will be exactly -1 or 1.
  • When sorting thousands of items, create const collator = new Intl.Collator("de") once and pass collator.compare to sort(). That is faster than calling localeCompare() repeatedly.
  • Always pass a language code. Without one, the result depends on the browser's settings and can differ from one computer to another.

Related entries

#

Spotted a mistake or missing an example? Write to us.