Qt Chinese sorting

Source: Internet
Author: User
Tags collator

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.