Qt Chinese strings are sorted by pinyin

target expectation

Sort a string of unordered Chinese city names in pinyin order.
insert image description here


accomplish

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]+") );
}

Guess you like

Origin blog.csdn.net/locahuang/article/details/126750019
Recommended