Hello,
how can i sort a QHash by the int value?
qSort doesn't work with QHash.
thanks
Niko
Hello,
how can i sort a QHash by the int value?
qSort doesn't work with QHash.
thanks
Niko
Hashes have no order. You can create a list of pairs and sort it.
niko (11th September 2006)
You can't sort QHash. From docs:
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.When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered.
EDIT:
Although Jacek gave better solution![]()
The Wheel weaves as the Wheel wills.
thanks for the answers!
you mean like this?Hashes have no order. You can create a list of pairs and sort it.
Qt Code:
class ColorCount { public: QRgb rgb; int count; operator <(); }; QList<ColorCount> colors; ... qSort(colors);To copy to clipboard, switch view to plain text mode
i thought about that too allready, but adding values to the list looks quite difficult and expensive to me, with QHash i could do:
and with the list i have to search everytime through the whole list to find the right entry.Qt Code:
QHash<QRgb, int> colors; colors[QRgb(...)]++;To copy to clipboard, switch view to plain text mode
..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
Or create that list after you have finished colour counting.Originally Posted by niko
yes, thats easier;
thx
Bookmarks