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. :)
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. :)