Results 1 to 5 of 5

Thread: Sorting a QStringlist issue

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sorting a QStringlist issue

    As yogeshgokul said the list is sorted well (lexicographically). The sorting you want you can achieve with custom comparator and use it with qSort():
    Qt Code:
    1. struct MyLessThan {
    2. bool operator()(const QString &s1, const QString &s2) const {
    3. QString st1 = s1.left(s1.indexOf('_'));
    4. int it1 = s1.right(s1.size() - s1.indexOf('_') - 1).toInt();
    5. QString st2 = s2.left(s2.indexOf('_'));
    6. int it2 = s2.right(s2.size() - s2.indexOf('_') - 1).toInt();
    7. if (st1 < st2)
    8. return true;
    9. if (st1 > st2)
    10. return false;
    11. if (it1 < it2)
    12. return true;
    13. return false;
    14. }
    15. };
    To copy to clipboard, switch view to plain text mode 
    than you can use it like this:
    Qt Code:
    1. slist << "A_8" << "A_2" << "A_10" << "A_24" << "A_1";
    2. MyLessThan le;
    3. qSort(slist.begin(), slist.end(), le);
    4. foreach (const QString &str, slist) {
    5. ui->plainTextEdit->appendPlainText(str);
    6. }
    To copy to clipboard, switch view to plain text mode 
    that gives me output:
    A_1
    A_2
    A_8
    A_10
    A_24
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  2. The following 2 users say thank you to faldzip for this useful post:

    George Neil (29th September 2009), zeFree (3rd December 2013)

Similar Threads

  1. QStringList Sorting
    By QbelcorT in forum Qt Programming
    Replies: 19
    Last Post: 29th August 2011, 12:29
  2. QTable Sorting
    By mromey in forum Qt Programming
    Replies: 1
    Last Post: 23rd April 2009, 07:28
  3. QStringList
    By jaca in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2008, 10:12
  4. QStringList in QObject::connect
    By DPinLV in forum Qt Programming
    Replies: 6
    Last Post: 6th September 2006, 17:01
  5. Cannot queue arguments of type 'QStringList'
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2006, 20:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.