Sorting C++ map by value

Status
Not open for further replies.
Yes. There is definitly a way to sort a map by the value rather than the key. However, since I'm on vacation, I don't have access to my CUJ that describes how to do it. I'll post a follow up once I get back (in 10 days or so) with an example of how to do it. This method does not make use of a secondary container (like a vector). The article in question is a monthly column called "The Standard Librarian" written by Matt Austern. I cannot remember which issue talked about sorting a map by value.
 
I was thinking that you could make your values the keys and vice versa. I chose a multimap figuring that your values may not have been unique, so if you used them as keys the map would need to be able to accomodate this.

Perhaps a better option is to use a set instead. Just put your key and value in a struct instead of as a std::pair, like a map does. Then you could overload the less than operator and sort the data anyway you want.
 

-Triple-

Ars Tribunus Angusticlavius
8,554
Perhaps a better option is to use a set instead. Just put your key and value in a struct instead of as a std::pair, like a map does. Then you could overload the less than operator and sort the data anyway you want.

I'm intelligent enough to create workarounds; however, I used a vector, not a set, for various reasons. Thanks for the suggestion, though.

ItaniumOxide: I'm very interested in seeing this. I couldn't find any solutions by Googling. I'll try searching directly for that column...
 
Status
Not open for further replies.