Results 1 to 8 of 8

Thread: Sort a QHash<QString, int> by value

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Sort a QHash<QString, int> by value

    There are multiple solutions for you:
    • If you can use SQL, create a SQLite database in the memory and use it. (best if your list/hash will be big)
    • After you have counted your words in a (temporary) QHash, transform it to a list of QPair's and sort it
    • ... or create a QMap with swaped keys and values.

  2. The following user says thank you to Lykurg for this useful post:

    Trader (7th July 2010)

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

    Default Re: Sort a QHash<QString, int> by value

    Quote Originally Posted by Lykurg View Post
    After you have counted your words in a (temporary) QHash, transform it to a list of QPair's and sort it
    Lykurg, tks.

    I try:

    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. qSort(items.begin(), items.end(), sorting);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool sorting(const QPair<WordCount,int>& e1, const QPair<WordCount,int>& e2) {
    2. //if (e1.first.value() < e2.first.value()) return true; // *** Not Working ***
    3. return true;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Trader; 7th July 2010 at 04:39.

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

    Default Re: Sort a QHash<QString, int> by value

    (excluded)
    Last edited by Trader; 7th July 2010 at 04:40.

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

    Default Re: Sort a QHash<QString, int> by value

    (excluded)
    Last edited by Trader; 7th July 2010 at 04:40.

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

    Default Re: Sort a QHash<QString, int> by value

    Thank you all, the problem has been resolved.

Similar Threads

  1. QHash<QString, QVarant> to file
    By weaver4 in forum Newbie
    Replies: 2
    Last Post: 9th February 2010, 06:54
  2. how can i destruct QHash<QString, QPixmap>?
    By SamSong in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2010, 06:41
  3. How does QString::qHash() work?
    By Morea in forum Qt Programming
    Replies: 4
    Last Post: 7th September 2009, 09:17
  4. QDataStream and QHash<QString,QVariant> crash on read
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2008, 12:14
  5. Sort a QHash<QRgb, int> by value
    By niko in forum Newbie
    Replies: 5
    Last Post: 11th September 2006, 10:11

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.