QT meeGo 中文字符串排序

 

Qt类中的qSort()函数提供了对字符串的排序功能。要利用qSort为中文进行排序则需要我们提供一个针对中文比较规则的比较器。

Meego Touch Framework 中的MCollator实现了该功能。更准确地说是MCollator实现了所有国家语言的排序功能。

简单的用法如下。

MLocale loc; // 这里设置对应国家的语言和排序方法
MCollator comp = loc.collator();

QStringList stringList;
//add contents to stringList
qSort(stringList.begin(), stringList.end(), comp); // sorts the list

中文有按照拼音(pinyin)和笔画(stroke)两种排序方式.

所以我们构造MLocale的时候可以用

//根据拼音或笔画选择一种
MLocale loc(“zh_CN@collation=pinyin”);
MLocale loc(“zh_CN@collation=stroke”);

完整的代码如下

#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;
}

如果是按拼音排序输出将是 (“策”, “党”, “的”, “克”, “西”, “亚”, “政”).

而按笔画排序输出将是 (“西”, “克”,”的”,”政”, “党”, “策”, “亚”).

需要注意的是编译该代码需要在你的工程文件.pro中加入CONFIG+=meegotouch.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值