如何在 C++ 中对字符串数组进行排序以使其按以下顺序发生:
安卡先生
布朗先生
塞瑟先生
mR 驴
奥利什先生
阿特先生
先生 önD
//following not the way to get that order regardeless upper or lowercase and å, ä, ö
//in forloop...
string handle;
point1 = array1[j].find_first_of(' ');
string forename1(array1[j].substr(0, (point1)));
string aftername1(array1[j].substr(point1 + 1));
point2 = array1[j+1].find_first_of(' ');
string forename2(array1[j+1].substr(0, (point2)));
string aftername2(array1[j+1].substr(point2 + 1));
if(aftername1 > aftername2){
handle = array1[j];
array1[j] = array1[j+1];
array1[j+1] = handle;//swapping
}
if(aftername1 == aftername2){
if(forname1 > forname2){
handle = array1[j];
array1[j] = array1[j+1];
array1[j+1] = handle;
}
}
最佳答案
一旦将 Unicode 字符混入其中,您就必须开始考虑国际化。不同的语言有不同的排序规则。例如,在荷兰语中,“IJ”被视为单个字母,在字母表中有自己的位置。我推荐一个很好的 Unicode 库来进行字符串词法比较,即 International Components for Unicode:http://site.icu-project.org/
有了它,您可以简单地将普通的 std::sort
与 ICU 的比较器一起使用。
关于c++ - 在 C++ 中对字符串数组进行排序,无论是 'A' 还是 'a' 以及 å、ä ö?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2494044/