The qsort () function in the QT class provides the function of sorting strings. To use qsort to sort Chinese, we need to provide a comparator for Chinese Comparison rules.
Mcollator in meego touch framework implements this function. More accurately, mcollator enables the sorting of languages in all countries.
The simple usage is as follows.
Mlocale LOC; // set the language and sorting method of the corresponding country.
Mcollator comp = LOC. collator ();
Qstringlist stringlist;
// Add contents to stringlist
Qsort (stringlist. Begin (), stringlist. End (), comp); // sorts the list
The Chinese character can be sorted by pinyin or stroke.
So we can use
// Select one type based on pinyin or strokes
Mlocale LOC ("zh_cn @ collation = pinyin ");
Mlocale LOC ("zh_cn @ collation = stroke ");
The complete code is as follows:
# Include <qcoreapplication>
# Include <qobject>
# Include <mlocale>
# Include <mcollator>
# Include <qstringlist>
# Include <qdebug>
# Include <qtextcodec>
Int main (INT argc, char * argv []) {
Qcoreapplication app (argc, argv );
// Mlocale LOC (locale_name );
Mlocale LOC ("zh_cn @ collation = pinyin ");
Mcollator mcomp = LOC. collator ();
Qtextcodec * Tc = qtextcodec: codecforname ("utf8 ″);
Qtextcodec: setcodecforcstrings (TC );
Qstringlist stringlist;
// Stringlist <"BB" <"da" <"AA" <"AB ";
Stringlist <"" <" ";
Qsort (stringlist. Begin (), stringlist. End (), mcomp );
Qdebug () <stringlist;
}
If the output is sorted by pinyin, it will be ("CE", "party", "of", "Ke", "West", "Asia", and "Politics ").
The output will be sorted by strokes ("West", "Ke", "," political "," party "," policy ", and" sub ").
Note that you need to add config + = meegotouch to your project file. pro to compile the code.
To be verified