PDA

View Full Version : How to qSort a Qlist of QPair?



Trader
7th July 2010, 12:58
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.



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); // :confused:

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

Ginsengelf
7th July 2010, 14:11
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

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