Results 1 to 5 of 5

Thread: QSort

  1. #1
    Join Date
    Dec 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QSort

    Hi,

    I have a problem with qSort, illustrated as follows:

    Qt Code:
    1. #include <qhash.h>
    2.  
    3. struct TDATA {
    4. QString emp_name;
    5. float salary;
    6. };
    7.  
    8. QHash < uint, TDATA> emps;
    9.  
    10. bool salaryLessThan(const TDATA &e1, const TDATA &e2) {
    11. return (e1.salary < e2.salary);
    12. }
    13.  
    14. int main(int argc, char *argv[])
    15. {
    16.  
    17. qStableSort(emps.begin(), emps.end(), salaryLessThan);
    18. return 1;
    19. }
    To copy to clipboard, switch view to plain text mode 
    Basically, I want to sort based on the values in the Hash.

    Compiling this gives a long error message as attached:

    Can anyone please help me out?

    Thanks.
    Attached Files Attached Files
    Last edited by jpn; 2nd December 2008 at 09:25. Reason: missing [code] tags

  2. #2
    Join Date
    May 2007
    Posts
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSort

    You cannot use qStableSort to sort qHash because qHash does not have Random Access Iterators. If you want sorting by qStableSort you have to use qVector or qList.

    http://doc.trolltech.com/4.3/qtalgor...ml#qStableSort

    http://doc.trolltech.com/4.3/qtalgor...cess-iterators

  3. #3
    Join Date
    Dec 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSort

    Hi,

    Thanks for responding.

    Please take a look at http://doc.trolltech.com/4.4/qtalgorithms.html.

    It clearly states
    You can use these algorithms with any container class that provides STL-style iterators, including Qt's QList, QLinkedList, QVector, QMap, and QHash classes.
    I think it is a problem with my function definition. I have looked at similiar threads but could not figure out what was required since my function is global.

    Thanks again.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSort

    read this and maybe you understand why items in QHash can't be sorted Qt containers.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Dec 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSort

    Got it!
    Thanks to spirit & pgorszkowski.

Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 18:55
  2. qSort with lessThan
    By soul_rebel in forum Qt Programming
    Replies: 4
    Last Post: 19th August 2008, 18:14
  3. qSort() problem
    By darksaga in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2007, 11:47
  4. qSort doesn't work with member function
    By ber_44 in forum Qt Programming
    Replies: 10
    Last Post: 2nd June 2007, 12:00
  5. [Qt4] QSORT
    By jane in forum Qt Programming
    Replies: 6
    Last Post: 24th May 2006, 23:38

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.