compareAsciiUpperCaseNatural function
Compares strings a
and b
according to upper-case
natural sort ordering.
ASCII letters are converted to upper case before being compared, like for compareAsciiUpperCase, then the result is compared like for compareNatural.
If two strings differ only on the case of ASCII letters, the one with the capital letter at the first difference will compare as less than the other string. This tie-breaking ensures that the comparison is a total ordering on strings
Implementation
int compareAsciiUpperCaseNatural(String a, String b) {
int defaultResult = 0;
for (int i = 0; i < a.length; i++) {
if (i >= b.length) return 1;
var aChar = a.codeUnitAt(i);
var bChar = b.codeUnitAt(i);
if (aChar == bChar) continue;
int aUpperCase = aChar;
int bUpperCase = bChar;
if (_lowerCaseA <= aChar && aChar <= _lowerCaseZ) {
aUpperCase -= _asciiCaseBit;
}
if (_lowerCaseA <= bChar && bChar <= _lowerCaseZ) {
bUpperCase -= _asciiCaseBit;
}
if (aUpperCase != bUpperCase) {
return _compareNaturally(a, b, i, aUpperCase, bUpperCase);
}
if (defaultResult == 0) defaultResult = aChar - bChar;
}
if (b.length > a.length) return -1;
return defaultResult.sign;
}