c++ - 在 C++ 中对字符串数组进行排序,无论是 'A' 还是 'a' 以及 å、ä ö?

标签 c++ string sorting arrays

如何在 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/

相关文章:

c++ - 修复了 boost::posix_time::ptime 的精度

c++ - 如何去除 canny 图像中的直线或非曲线

c++ - clang-tidy:什么可能导致 NOLINT 评论不被尊重?

c# - 需要一些关于排序字符串列表的想法

python - 查找排序列表的两个数字之间是否存在数字的更好方法

c++ - SSL_shutdown() 返回 -1 且 errno 为 0

swift - 如何将字符串换行为 72 个字符

c# - 字符串操作和分割字符串

java - 如何在不使用 collections.sort() 的情况下按字母顺序对数组列表进行排序;

postgresql - 使用层次结构对列进行排序,并使用附加列进行排序