Although, Lucie's answer works, I ended up writing my own extension for strings for Turkish alphabet operations for the general use. The Turkish alphabet has a weird issue with capital letter "I". Its lowercase form is "ı" (not "i"). Also, the lowercase letter "i" has an uppercase form as "İ". Dart does not support this minor detail for the Turkish in its toLowerCase
function.
Sorting strings with Turkish characters can be done with the snippet below. (based on Daniel's answer):
extension TurkishStringOperations on String {
String toTurkishLowerCase() {
return replaceAll("İ", "i")
.replaceAll("Ş", "ş")
.replaceAll("Ç", "ç")
.replaceAll("Ö", "ö")
.replaceAll("I", "ı")
.replaceAll("Ü", "ü")
.replaceAll("Ğ", "ğ")
.toLowerCase();
}
String toTurkishUpperCase() {
return replaceAll("i", "İ")
.replaceAll("ş", "Ş")
.replaceAll("ç", "Ç")
.replaceAll("ö", "Ö")
.replaceAll("ı", "I")
.replaceAll("ü", "Ü")
.replaceAll("ğ", "Ğ")
.toUpperCase();
}
int turkishCompareTo(String other) {
var letters = [
"a", "b", "c", "ç", "d", "e", "f", "g", "ğ", "h", "ı", "i", "j", "k",
"l", "m", "n", "o", "ö", "p", "r", "s", "ş", "t", "u", "ü", "v", "y",
"z", "w", "q", "x",
];
var that = toTurkishLowerCase();
other = other.toTurkishLowerCase();
for (var i = 0; i < min(that.length, other.length); i++) {
var thatValue = letters.indexOf(that[i]);
var otherValue = letters.indexOf(other[i]);
var result = (thatValue - otherValue).sign;
if (result != 0) {
return result;
}
}
return (that.length - other.length).sign;
}
}
The example list below is sorted with a standard sort()
method of list using our extension method:
var countries = ["Belgrad", "Belçika", "Irak", "İran", "Sudan", "Şili", "Çek Cumhuriyeti", "Cezayir", "Ukrayna", "Ürdün"];
countries.sort((firstString, secondString) => firstString.compareTo(secondString));
print(countries); // prints in a wrong order: [Belgrad, Belçika, Cezayir, Irak, Sudan, Ukrayna, Çek Cumhuriyeti, Ürdün, İran, Şili]
countries.sort((firstString, secondString) => firstString.turkishCompareTo(secondString));
print(countries); // prints in a correct Turkish order: [Belçika, Belgrad, Cezayir, Çek Cumhuriyeti, Irak, İran, Sudan, Şili, Ukrayna, Ürdün]