PDA

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



niko
10th September 2006, 20:36
Hello,

how can i sort a QHash by the int value?
qSort doesn't work with QHash.

thanks
Niko

jacek
10th September 2006, 21:38
Hashes have no order. You can create a list of pairs and sort it.

danadam
10th September 2006, 21:44
You can't sort QHash. From docs:

When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered.

If you want to have elements of QHash<QRgb, int> sorted, you could copy them to QMap<int, QRgb>. In that way you'll have them sorted by int.

EDIT:
Although Jacek gave better solution :o

niko
11th September 2006, 06:23
thanks for the answers!


Hashes have no order. You can create a list of pairs and sort it.
you mean like this?

class ColorCount {
public:
QRgb rgb;
int count;
operator <();
};
QList<ColorCount> colors;
...
qSort(colors);


i thought about that too allready, but adding values to the list looks quite difficult and expensive to me, with QHash i could do:

QHash<QRgb, int> colors;
colors[QRgb(...)]++;

and with the list i have to search everytime through the whole list to find the right entry.

..although i could create a temporary QHash that just stores the position of the color in the QList... i guess the would speed it up again!


thx
niko

jacek
11th September 2006, 09:32
although i could create a temporary QHash that just stores the position of the color in the QList
Or create that list after you have finished colour counting.

niko
11th September 2006, 10:11
yes, thats easier;

thx