LocalCompare can be case-sensitiv, enabled by { sensitivity: 'case'}
or the unicode extension: u-kf-upper
or u-kf-lower
.
The spec of LocalCompare says, that:
The result is intended to order String values in the sort order
specified by the system default locale
Or the locale you can add as argument.
The default for en-US for the case sensitive order is "lower" with:
aAbBcC ...
You could change it to "upper", which is:
AaBbCc ...
This test shows that switching from upper
to lower
does only affect the same letter, and does not sort uppercase-alphabet before lowercase or visa versa.
var b = "A".localeCompare("a", 'en-US-u-kf-lower'); // 1
var c = "A".localeCompare("a", 'en-US-u-kf-upper'); // -1
var d = "A".localeCompare("b", 'en-US-u-kf-upper'); // -1
var e = "A".localeCompare("b", 'en-US-u-kf-lower'); // -1
- There might be a locale that sorts uppercase/lowercase differently, but I'm not aware of any...
- the international sort order is case insensitive
- ASCII sort order would be
ACb
- Some locales have different orders: Finnish for example treads V like W:
Va Wb Vc
...
Does case-sensitivity affect numeric values in any way?
var b = "1".localeCompare("a", 'en-US-u-kf-lower'); // -1
var c = "1".localeCompare("A", 'en-US-u-kf-upper'); // -1
var d = "104".localeCompare("a", 'en-US-u-kf-upper'); // -1
no, but ...
there is an option, how numeric values can be compared: kn
with the option or Unicode extension kn
one can enable numeric comparison (kn-true
):
var b = "10".localeCompare("2", 'en-US-u-kn-true'); // 1
var c = "10".localeCompare("2", 'en-US'); // -1
However, numbers are always before letters:
var d = "1".localeCompare("a", 'en-US-u-kn-true'); // -1
var e = "1".localeCompare("a", 'en-US'); // -1
locales
argument, but, at least with"en"
that didn't make a difference. Also tried the"caseFirst"
option, but that didn't make a difference either, although it does say Implementations are not required to support this property. – Heretic Monkeyb
occur beforeA
in the alphabetic order? – TeemuAaBbCcDd ...
. You can switch it toaAbBcCdD
usinglocaleCompare
though. – Teemu