Results 1 to 8 of 8

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

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

    Question Sort a QHash<QString, int> by value

    I need to sort QHash.

    In java (HashMap) (code below) works correctly.

    Someone can please help me translate this Java code to Qt?

    Main.java
    Qt Code:
    1. import java.util.ArrayList;
    2. import java.util.Collections;
    3. import java.util.HashMap;
    4.  
    5. public class Main {
    6. public static void main(String[] args) {
    7. HashMap<String ,Word> h = new HashMap<String, Word>();
    8.  
    9. popHash(h); // I have a hash and want to order it by the value of each key.
    10.  
    11. ArrayList<Word> aux = new ArrayList<Word>(); // QList<Word> list;
    12.  
    13. for(String s:h.keySet()){
    14. aux.add(h.get(s));
    15. }
    16.  
    17. Collections.sort(aux); // qsort(list);
    18.  
    19. for(Word p:aux){
    20. System.out.println(p);
    21. }
    22. }
    23.  
    24. private static void popHash(HashMap<String, Word> h) {
    25. String[] word = {"not", "using", "Qt", "but", "in", "Hash", "Java", "know", "sort", "I"};
    26. int[] cont = {2, 5, 0, 3, 1, 6, 4, 8, 7, 9};
    27. Word p;
    28.  
    29. for(int i=0; i<cont.length; i++){
    30. p = new Word();
    31. p.setWord(word[i]);
    32. p.setCount(cont[i]);
    33. h.put(p.getWord(), p);
    34. }
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    /* Output:
    9 I
    8 know
    7 sort
    6 Hash
    5 using
    4 Java
    3 but
    2 not
    1 in
    0 Qt
    */


    Word.java
    Qt Code:
    1. public class Word implements Comparable<Word> {
    2. private String word;
    3. private int count;
    4.  
    5. public String getWord() {
    6. return word;
    7. }
    8.  
    9. public void setWord(String word) {
    10. this.word = word;
    11. }
    12.  
    13. public int getCount() {
    14. return count;
    15. }
    16.  
    17. public void setCount(int count) {
    18. this.count = count;
    19. }
    20.  
    21. // How do I do this comparison function in Qt? - operator <(Word &p) not working
    22. @Override
    23. public int compareTo(Word o) {
    24. return o.count - this.count;
    25. }
    26. @Override
    27. public String toString(){
    28. return this.count +" "+this.word;
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    Thank you very much.

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

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

    sort a QHash is a little bit "nonsense". Better use Q(Multi)Map for that since its items are automatically sorted. If your really need a QHash, then see qSort() (= qSort ( RandomAccessIterator begin, RandomAccessIterator end, LessThan lessThan )). But that is not the best approach!

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

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

    Hello Lykurg

    I used (QHash) to do a word count.

    I have to order the Value (words that appear most), not the Key.

    So the QMap not solve this problem, because it orders the Key.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    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.

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

    Trader (7th July 2010)

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

    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 05:39.

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

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

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

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

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

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

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

    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, 07:54
  2. how can i destruct QHash<QString, QPixmap>?
    By SamSong in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2010, 07:41
  3. How does QString::qHash() work?
    By Morea in forum Qt Programming
    Replies: 4
    Last Post: 7th September 2009, 10:17
  4. QDataStream and QHash<QString,QVariant> crash on read
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2008, 13:14
  5. Sort a QHash<QRgb, int> by value
    By niko in forum Newbie
    Replies: 5
    Last Post: 11th September 2006, 11: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.