I was trying what I hoped to be a trivial exercise in coding: sorting Javascript strings with an ASCII style lexicographical order (e.g. numbers before capitalized letters before lowercased letters...).
Here's a snippet:
var str1 = "ab";
var str2 = "Ab";
var n = str1.localeCompare(
str2, "en", {sensitivity: 'variant', caseFirst: "upper"}
);
In this case, I would expect n
to be 1
, but it returns -1
instead.
From the documentation on this page:
- the
sensitivity
value set asvariant
would allow differentiating between all base and accented letters, including case - the
caseFirst
value set asupper
would force upper-cased letters to compare smaller than lower-cased letters - the lack of
usage
parametrization would default tosort
, which would be irrelevant here anyway since I'm specifyingvariant
- the lack of
ignorePunctuation
parametrization would default tofalse
I am assuming the options override the default locale settings, although I couldn't find any specific information on the matter.
In truth if it defaulted to en-US
and had priority over the options, then I imagine case would be ignored (e.g. see accepted answer here).
What am I doing wrong?
Notes
- I am mentioning "ASCII" here for the sole purpose of identifying a sorting order that does not ignore case and sorts uppercase letters before lowercase letters where applicable. I would ultimately employ this for unicode strings as well.
- As suggested by some, this is likely engine-dependent. Replicated with Firefox ESR 52.6.0, and Chromium 64.0.3282.167.