Sorting QStringList using qSort

  • Hi,

    I am suing QStringList to store the data. QStringList having the following item.
    QStringList list<<"user1"<<User2"<<"User7"<<"User10"<<"user6".

    I am using qSort() to sort the list as follows.

    qSort(list.begin(), list.end(), caseInsensitiveLessThan);

    bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
    return s1.toLower() < s2.toLower();

    But it give the sortine order as "user1"<<User10"<<"User2"<<"user6"<<"User7".

    But i want the following order after sorting.


    Can any one of you give solution to solve the problem its very helpful.

    Thanks in advance

  • Moderators

    There is no one-liner for that. You need a better comparison function called "natural comparison" i.e. not how a computer understands order but how humans do.

    Here's a nice implementation of such algorithm with code for various languages:
    You should be able to easily modify it to fit with qSort.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.