Results 1 to 3 of 3

Thread: How to qSort a Qlist of QPair?

  1. #1
    Join Date
    May 2010
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to qSort a Qlist of QPair?

    Hi guys,

    I'm trying to order a QList of QPair <QHash <QString, int>, int> (ordered by the value of QHash).

    I do not know if this form is correct, but is not giving error.

    I am not able to create the qSort(items.begin(), items.end(), sorting); and their method of ordering (sorting) and do not know if I have to use the operator<

    Could anyone help me?

    Thank you.

    Qt Code:
    1. typedef QHash<QString, int> WordCount;
    2. ....
    3.  
    4. QList<QPair<WordCount,int> > items;
    5. QPair<WordCount,int> pair;
    6. pair.first = myHash;
    7. pair.second = totalKeys;
    8. items.append(pair);
    9.  
    10. qSort(items.begin(), items.end(), sorting); // :confused:
    11.  
    12. bool sorting(const QPair<WordCount,int>& e1, const QPair<WordCount,int>& e2) { // sorting by QHash Value
    13. //if (e1.first.value() < e2.first.value()) return true; // *** Not Working *** :confused:
    14. return true;
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2008
    Posts
    439
    Thanks
    9
    Thanked 57 Times in 55 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to qSort a Qlist of QPair?

    Hi, e1.first.value(KEY) does not return the hash value, but the value stored in the QHash which corresponds to KEY. QHash essentially works like a map. It uses the hash value for each key/value pair to get a faster lookup, so there is no single hash value for a QHash object.

    Ginsengelf

  3. #3
    Join Date
    May 2010
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to qSort a Qlist of QPair?

    Thank you all, the problem has been resolved.

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. Replies: 10
    Last Post: 20th June 2010, 22:29
  3. Replies: 4
    Last Post: 12th October 2009, 19:36
  4. why doesn't qSort work when a QList hosts a pointertype?
    By momesana in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2009, 04:28
  5. using QPair in QVector
    By damonlin in forum Qt Programming
    Replies: 5
    Last Post: 28th January 2009, 09:29

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.