Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sorting QStringList using qSort
QtWS25 Last Chance

Sorting QStringList using qSort

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    econtech
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved