开发者

How to Sort UTF-8 String which Contains Digits & Characters?

I am working on Program(in c) which require 开发者_如何学编程sorting. One of the requirement of sorting is : Digits Sorting.

Digit sorting shall be completed from least significant digit (i.e. the rightmost digit) and to the most significant digit (i.e. the leftmost digit) such that the numbers 21, 2, and 11 are sorted as follows: 2, 11, 21.

The given string is in UTF-8 and may contains Special Characters,Digits,Latin letters ,Cyrillic letters ,Hiragana/Katakana etc.

It give following sorting Order :

1

1a

1b

2

11

110

110a

Henry7

Henry24


You might want to consider using the ICU library (International Components for Unicode), which includes a collation (sorting) API.


I think you mean "sort numerical characters in text strings as numbers." You may try using Qt's QString::localeAwareCompare() which makes use of locale and platform settings to compare strings. At least on OS X, this should mean it will respect the user selected locale which include the behavior you want.


Or you can convert it to utf16 and sort by code point value if you don't care about locale.


Use std::sort's custom comparator function by checking with QString::localeAwareCompare().

Comparator function:

void sortLocaleAware(QStringList &sList)
{
    std::sort(sList.begin(), sList.end(), [](const QString &s1, const QString &s2){
        return s1.localeAwareCompare(s2) < 0;
    });
}

Usage:

QStringList myList = { "4a", "3b", "52a" ,"13ş", "34İ" };
sortLocaleAware(myList);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜