Results 1 to 5 of 5

Thread: Sorting a QStringlist issue

  1. #1
    Join Date
    Jun 2008
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Sorting a QStringlist issue

    I tried the following code,

    QStringList oStrlist;
    oStrlist << "A_1" << "A_2" << "A_3" << "A_10" << "A_11";
    oStrlist.sort();

    This gives me a result like
    A_1, A_10, A_11, A_2, A_3.

    I need A_10, A_11 to come only after A_2, A_3. Is there any way to do this.

  2. The following user says thank you to George Neil for this useful post:

    zeFree (3rd December 2013)

  3. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sorting a QStringlist issue

    Quote Originally Posted by George Neil View Post
    I tried the following code,
    QStringList oStrlist;
    oStrlist << "A_1" << "A_2" << "A_3" << "A_10" << "A_11";
    oStrlist.sort();
    This gives me a result like
    A_1, A_10, A_11, A_2, A_3.
    I need A_10, A_11 to come only after A_2, A_3. Is there any way to do this.
    According to you, the list is sorted already
    You can always do custom sorting. Click here for more info.

  4. #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.

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

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

  6. #4
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sorting a QStringlist issue

    Quote Originally Posted by faldżip View Post
    As yogeshgokul said the list is sorted well (lexicographically).
    See his input itself sorted

    Quote Originally Posted by George Neil View Post
    QStringList oStrlist;
    oStrlist << "A_1" << "A_2" << "A_3" << "A_10" << "A_11";
    oStrlist.sort();
    @Falzip
    Nice phrase
    lexicographically

  7. #5
    Join Date
    Jun 2008
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: Sorting a QStringlist issue

    Great....that works...!!

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.