String.localeCompare

localeCompare — Compare two stings based on locale rules.

Description

String.localeCompare(string string, [string locales, [object options]])

Parameters

Name Description Type Default Optional
string string No
locales string Yes
options See options below. object Yes
Options parameter

The options parameter may contain one or more of the following data items.

localeMatcher

Possible values are: "best fit" (default) or "lookup".

usage

Possible values are: "sort" (default) or "search".

sensitivity

Possible values are: "base", "accent", "case", or "variant" (default).

ignore­Punctua­tion

Possible values are: true or false (default).

numeric

Possible values are: true or false (default).

caseFirst

Possible values are: "upper", "lower" or "false" (default).

Return values

number primitive.

Examples

Example #1 – localeCompare example

In English the letter "å" comes before "z", while in Norwegian it comes after.

console.log('å'.localeCompare('z', 'en')); // -1 (or some other negative value) console.log('å'.localeCompare('z', 'nb')); // 1 (or some other positive value)

Equal compare will return a zero.

console.log('z'.localeCompare('z', 'en')); // 0

External references