View Full Version : How to sort QMap

9th November 2007, 10:51
I have a QMap<QString, QString> allSources in witch key is a "table_by_view" names, as vlues is "view_title".
"Activity_view", "Activiti";
"car_specification_view", "cars";

And so on.

But now I need to sord by order "view_title". A/b/s and so on..
How can I do this, for get :
"pOrders_view", "orders";

Now i get only values as QStringList , sorting it and geting values in right order, but key in old order...

9th November 2007, 10:54
A map is an associative container, it stores its values sorted by keys to improve access speed to values. There is no way of sorting a map by value order. You have to use QList<QPair<x,y>> instead.

9th November 2007, 11:11
or, if you need only sorted titles without associations to keys, use

QStringList list = QMap::values();
qSort(list.begin(), list.end());

U may get associated keys by calling QMap::key(const T & value );