Qt中文字符串按照拼音排序

目标期望

对一串无序的中文城市名称按照拼音顺序排序。
在这里插入图片描述


实现

void sort()
{
    
    
	QStringList stringList;
    stringList<<"广州"<<"成都"<<"北京"<<"上海"<<"杭州"<<"洛阳"<<"中国";

    QMap<QByteArray,QString> barryMap;
    QTextCodec* codec = QTextCodec::codecForName("GBK");
    if(codec)
    {
    
    
       for(int i=0; i<stringList.count(); i++)
       {
    
    
           QString text = stringList.at(i);
           if( isContainsHz(text))
           {
    
    
               QByteArray barr = codec->fromUnicode(text);
               barryMap.insert(barr, text);
           }
           else
           {
    
    
               barryMap.insert(text.toLatin1(), text);
           }
       }
    }
    stringList.clear();
    stringList = barryMap.values();
}

bool isContainsHz(const QString text)
{
    
    
    return text.contains( QRegExp("[\\x4e00-\\x9fa5]+") );
}

猜你喜欢

转载自blog.csdn.net/locahuang/article/details/126750019