Results 1 to 6 of 6

Thread: Sort QList of pointers

  1. #1
    Join Date
    Jan 2011
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6

    Default Sort QList of pointers

    I have a QList of QTcpSocket derived class pointers, which have a custom field for transfer speed and I need to sort list by this field. It is easy to use function qSort for list of values, but I haven't found how not apply this func for list of pointers.

  2. #2
    Join Date
    Aug 2008
    Posts
    45
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Sort QList of pointers

    Maybe you could use this version of the qSort?
    Qt Code:
    1. void qSort ( RandomAccessIterator begin, RandomAccessIterator end, LessThan lessThan )
    To copy to clipboard, switch view to plain text mode 
    And use your own less than comparison function where you compare the fields.

  3. #3
    Join Date
    Jan 2011
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6

    Default Re: Sort QList of pointers

    Quote Originally Posted by joyer83 View Post
    Maybe you could use this version of the qSort?
    Qt Code:
    1. void qSort ( RandomAccessIterator begin, RandomAccessIterator end, LessThan lessThan )
    To copy to clipboard, switch view to plain text mode 
    And use your own less than comparison function where you compare the fields.
    I know about this method, but I don't know how to correctly write this function for accepting pointers.

    Qt Code:
    1. bool lessThan(MyClass *first, MyClass *second)
    To copy to clipboard, switch view to plain text mode 

    Doesn't work.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Sort QList of pointers

    Seems to work fine for me:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. struct A {
    4. A(int _x) : x(_x){}
    5. int x;
    6. };
    7.  
    8. bool lessThan(A *one, A*two){
    9. return one->x < two->x;
    10. }
    11.  
    12. int main(int argc, char **argv){
    13. QList<A*> list;
    14. for(int i=0;i<100;++i){
    15. list << new A(qrand()%1000);
    16. }
    17. qSort(list.begin(), list.end(), lessThan);
    18. foreach(A *it, list) qDebug() << it->x;
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6

    Default Re: Sort QList of pointers

    Finally I found solution: just need to add STATIC keyword to function's declaration, e.g. for descending sort:

    Header:
    Qt Code:
    1. static bool compareSockets(ClientSocket *first, ClientSocket *second);
    To copy to clipboard, switch view to plain text mode 

    CPP:
    Qt Code:
    1. bool NetworkManager::compareSockets(ClientSocket *first, ClientSocket *second)
    2. {
    3. return first->getTransferSpeed() > second->getTransferSpeed();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Call:
    Qt Code:
    1. qSort(clientList->begin(), clientList->end(), compareSockets);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Sort QList of pointers

    Quote Originally Posted by giantdragon View Post
    Finally I found solution: just need to add STATIC keyword to function's declaration
    It's a method not a function. That's your problem -- lessThan is supposed to be a function.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to sort a Qlist AND get a sort key?
    By agerlach in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2010, 19:44
  2. QList with pointers item
    By paolom in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2009, 16:16
  3. QList with pointers
    By Rockem in forum Qt Programming
    Replies: 9
    Last Post: 12th August 2009, 19:53
  4. QList Pointers
    By coderbob in forum Newbie
    Replies: 2
    Last Post: 20th November 2007, 19:50
  5. QList of pointers
    By Farcaller in forum Newbie
    Replies: 4
    Last Post: 24th January 2006, 17:48

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.