CKTIS / JavaScript
String.prototype.localeCompare — locale-sensitive text comparison
String.prototype.localeCompare compares two strings using linguistic ordering rules. The sign of its result determines their order; locales and options customize comparison in environments supporting ECMA-402.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
string.localeCompare(compareString, locales?, options?)String.prototype.localeCompare compares its receiver string with compareString. A negative result means before the other string, a positive result means after it, and 0 means equivalence under that comparison, not necessarily identical text. Check the result's sign, not exact values of -1 and 1.
In environments with ECMA-402, locales requests a preferred language, such as "en"; omitting it selects the environment's default. options customizes comparison: numeric: true orders digit sequences by numeric value. Rules depend on available locale data; without ECMA-402, support for these arguments cannot be relied on.
For sorting, pass (a, b) => a.localeCompare(b, "en", options). Do not pass the method itself as the comparator: it needs the correct this receiver. In the example, numeric: true places item2 before item10; the expected line is item1, item2, item10.
Examples
const labels = ["item10", "item2", "item1"];
const options = { numeric: true };
labels.sort((a, b) => a.localeCompare(b, "en", options));
console.log(labels.join(", "));Result
item1, item2, item10
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.