Results 1 to 6 of 6

Thread: How to qSort a QList of a class

  1. #1
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to qSort a QList of a class

    I have a QList of Versions .. where Versions is a class and has two QStrings (text1 and text2)...
    how can I sort this QList by text1?

    Thanks

  2. #2

    Default Re: How to qSort a QList of a class

    QStringList::sort () ? Also, qSort from <QtAlgorithms> would be helpful.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to qSort a QList of a class

    Overload operator<() for Versions class and use qSort if you have to sort/compare values in multiple places, or use the qSort that takes a lessThan function if you only need the comparison in place for to perform the sort.

  4. #4
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to qSort a QList of a class

    thx for the replies!

    Can't really get it to work, may someone help me out?

    Qt Code:
    1. class Version{
    2. public:
    3. Version(QString releaseDate,QString url);
    4. QString releaseDate;
    5. QString URL;
    6. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. QList<Version> allVersions;
    2. // adding some items through xml (irrelevant for this thread)
    3. // how do I sort this list now by the release date?
    To copy to clipboard, switch view to plain text mode 

    thanks!

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to qSort a QList of a class

    Have you at least read the links that i gave you in my previous post?
    Qt Code:
    1. class Version{
    2. public:
    3. Version(QString releaseDate,QString url) : releaseDate(releaseDate), URL(url) { }
    4. QString releaseDate;
    5. QString URL;
    6. };
    7.  
    8. bool VersionCompare(const Version& i, const Version& j)
    9. {
    10. return i.releaseDate < j.releaseDate; //what ever you consider necessary... for one Version to be lessThan other
    11. }
    12.  
    13. int main()
    14. {
    15. QList<Version> list;
    16.  
    17. list.append(Version("zz", "url"));
    18. list.append(Version("gg", "url"));
    19. list.append(Version("aa", "aa"));
    20.  
    21. qSort(list.begin(), list.end(), VersionCompare);
    22.  
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to qSort a QList of a class

    of course I did but I didn't get that I have to change the parameters in the compare function
    It's all clear for me now!
    Thanks

  7. The following user says thank you to TempleClause for this useful post:


Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 19:55
  2. Replies: 3
    Last Post: 6th April 2012, 17:44
  3. sorting static QList<MyClass> list with qSort
    By oruccim in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2010, 17:13
  4. How to qSort a Qlist of QPair?
    By Trader in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2010, 20:04
  5. why doesn't qSort work when a QList hosts a pointertype?
    By momesana in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2009, 05:28

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.