Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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.

    "user1"<<"User2"<<"user6"<<"User7"<<"User10".

    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: http://www.davekoelle.com/alphanum.html
    You should be able to easily modify it to fit with qSort.


Log in to reply