PDA

View Full Version : Sort a QHash<QString, int> by value



Trader
6th July 2010, 16:22
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

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Main {
public static void main(String[] args) {
HashMap<String ,Word> h = new HashMap<String, Word>();

popHash(h); // I have a hash and want to order it by the value of each key.

ArrayList<Word> aux = new ArrayList<Word>(); // QList<Word> list;

for(String s:h.keySet()){
aux.add(h.get(s));
}

Collections.sort(aux); // qsort(list);

for(Word p:aux){
System.out.println(p);
}
}

private static void popHash(HashMap<String, Word> h) {
String[] word = {"not", "using", "Qt", "but", "in", "Hash", "Java", "know", "sort", "I"};
int[] cont = {2, 5, 0, 3, 1, 6, 4, 8, 7, 9};
Word p;

for(int i=0; i<cont.length; i++){
p = new Word();
p.setWord(word[i]);
p.setCount(cont[i]);
h.put(p.getWord(), p);
}
}
}

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


Word.java

public class Word implements Comparable<Word> {
private String word;
private int count;

public String getWord() {
return word;
}

public void setWord(String word) {
this.word = word;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

// How do I do this comparison function in Qt? - operator <(Word &p) not working
@Override
public int compareTo(Word o) {
return o.count - this.count;
}
@Override
public String toString(){
return this.count +" "+this.word;
}
}

Thank you very much. :)

Lykurg
6th July 2010, 17:14
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!

Trader
6th July 2010, 17:53
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.

Lykurg
6th July 2010, 22:59
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.

Trader
7th July 2010, 04:04
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:



typedef QHash<QString, int> WordCount;
....

QList<QPair<WordCount,int> > items;
QPair<WordCount,int> pair;
pair.first = myHash;
pair.second = totalKeys;
items.append(pair);
qSort(items.begin(), items.end(), sorting);




bool sorting(const QPair<WordCount,int>& e1, const QPair<WordCount,int>& e2) {
//if (e1.first.value() < e2.first.value()) return true; // *** Not Working ***
return true;
}

Trader
7th July 2010, 04:21
(excluded)

Trader
7th July 2010, 04:36
(excluded)

Trader
7th July 2010, 19:03
Thank you all, the problem has been resolved. :)