How to sort map based on its values

I have defined a map<string, int> where string are words and int are how many times they appear in a textfile.

Now I want to sort the according to descending order of it's frequency. I now for this I have to write my own comparison function, which will be use in sort.


But I am finding difficulty in this So I need help.... Thanks in advance
google for a bubble sort example.
Go to the page on here about the map's constructor.
Use a multimap of <int, string>.

Iterate through the original map, make_pair of <int, string>, and insert into the multimap.

The default multimap less sorts in ascending order. You can iterate through the multimap in reverse order.

Jiryih Tsaur
If you want a container in which you want to be able to sort on both key and value, the right container is a boost::multi_index_container. However, boost isn't for the faint of heart, and if this for a school project, then do as jtsaur said.

Topic archived. No new replies allowed.