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, and0when they are equivalent. - Changes the original
- No
Example
#const students = ['Özlem', 'Zoe', 'mia', 'Marco', 'Eva']
console.log([...students].sort())
console.log([...students].sort((a, b) => a.localeCompare(b, 'de')))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
#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, includingsensitivity("base","accent","case","variant") andnumeric(comparing numbers by value).
More examples
#console.log('a'.localeCompare('b', 'de'))
console.log('b'.localeCompare('a', 'de'))
console.log('cat'.localeCompare('cat', 'de'))
console.log('ö'.localeCompare('p', 'de') < 0)const answer = 'germany'
const correct = 'Germany'
console.log(answer === correct)
console.log(answer.localeCompare(correct, 'de', { sensitivity: 'base' }) === 0)const lessons = ['lesson10', 'lesson2', 'lesson1']
console.log([...lessons].sort())
console.log([...lessons].sort((a, b) => a.localeCompare(b, 'de', { numeric: true })))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-1or1. - When sorting thousands of items, create
const collator = new Intl.Collator("de")once and passcollator.comparetosort(). That is faster than callinglocaleCompare()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
#- Array.prototype.sort()Sorts the array’s elements in place and returns the same array.
- Array.prototype.toSorted()Returns a sorted copy of the array, leaving the original unchanged.
- String.prototype.toLowerCase()Returns a copy of the string converted to lowercase. Useful for comparisons and normalizing data.
- String.prototype.toUpperCase()Returns a copy of the string converted to uppercase. Handles accented letters correctly.
Spotted a mistake or missing an example? Write to us.