Results 1 to 5 of 5

Thread: Order QList alphabetically

  1. #1
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Order QList alphabetically

    Hi all!

    I have a qlist of a object that I created. The object contains this fields: GroupID, GroupInfo, Price and Hours.
    I need to sort the QList of this Group object by the GroupInfo alphabetic order. Is there a qt function to do this?

    Thanks!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Order QList alphabetically

    Assuming you have instance of the objects in the QList, not just pointers, then qSort(), qStableSort() or the STL equivalents. You will need to implement operator<() for the contained class or provide a functor.

  3. #3
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Order QList alphabetically

    Quote Originally Posted by ChrisW67 View Post
    Assuming you have instance of the objects in the QList, not just pointers, then qSort(), qStableSort() or the STL equivalents. You will need to implement operator<() for the contained class or provide a functor.
    thank you for you answer. I have a QList of pointer. Forgot to mention that. Does that mean I can use neither of the function you told me about?

  4. #4
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Order QList alphabetically

    Nevermind. I did a funcion to sort the QList by my self. I wish that Qt did that though.

    Thanks anyway

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Order QList alphabetically

    It is easy to sort collections of pointers using STL's sort method. You simply have to implement a comparison function that takes pointer arguments instead of references to object instances. Presumably you already had to do something like this in your own sort function.

    I wish that Qt did that though
    It does, exactly the way STL's sort does: there are two versions of qSort(), the second of which takes a comparison function as the third argument. So you simply write:

    Qt Code:
    1. bool myPointerLessThan( MyPointer * p1, MyPointer * p2 )
    2. {
    3. if ( p1 && p2 )
    4. return p1->someValue < p2->someValue;
    5. return false;
    6. }
    7.  
    8. QList< MyPointer * > myList;
    9. // ... fill the list somehow
    10.  
    11. qSort( myList.begin(), myList.end(), myPointerLessThan );
    To copy to clipboard, switch view to plain text mode 

    Can't get much easier than that.

Similar Threads

  1. Qlist<QLabel *> in Qlist<QAction*>
    By Naahmi in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2011, 08:53
  2. Cast QList<Foo*> to QList<const Foo*>
    By vfernandez in forum Qt Programming
    Replies: 0
    Last Post: 4th October 2010, 16:04
  3. Replies: 4
    Last Post: 20th August 2010, 13:54
  4. Copying an QList into a new, sorted QList.
    By Thomas Wrobel in forum Newbie
    Replies: 3
    Last Post: 11th January 2010, 18:27
  5. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42

Tags for this Thread

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.