compareDisplayNames method
Sort display names in the "natural" way, ignoring case and diacritics.
By default they're sorted lexicographically, which means E
comes before
e
, which itself comes before É
. The usual, more natural sorting,
ignores case and diacritics. For example Édouard Manet
should come
before Elon Musk
.
In addition, numbers come after letters.
Implementation
static int compareDisplayNames(Contact a, Contact b) {
var x = a.normalizedName;
var y = b.normalizedName;
if (x.isEmpty && y.isNotEmpty) return 1;
if (x.isEmpty && y.isEmpty) return 0;
if (x.isNotEmpty && y.isEmpty) return -1;
if (_alpha.hasMatch(x[0]) && !_alpha.hasMatch(y[0])) return -1;
if (!_alpha.hasMatch(x[0]) && _alpha.hasMatch(y[0])) return 1;
if (!_alpha.hasMatch(x[0]) && !_alpha.hasMatch(y[0])) {
if (_numeric.hasMatch(x[0]) && !_numeric.hasMatch(y[0])) return -1;
if (!_numeric.hasMatch(x[0]) && _numeric.hasMatch(y[0])) return 1;
}
return x.compareTo(y);
}