Sorting map elements by VALUE

Hello everybody!

I want to make a program that is able to sort names in alphabetical order(no, it's not a homework). Since my mother tongue is not English, but Hungarian, we have some characters that are in our alphabet at the beginning, but their value is more in the ASCII table, and some letters are made of two or three characters, so the normal sort function is not suitable for me.

So, I want to store the letters of the alphabet in a map<string, int> structure. I'm in the beginning phase of the program, and now I just want it to write to the screen the alphabet. I use const_iterator for going through the map, but the map stores the values ordered by the KEY. However the value of the map should "tell" the position of the letter in the alphabet. So, my question is: How to make the map order/sort the elements by the VALUE? I tried the reference here, I tried the book from Bjarne Stroustrup, but nothing helped, nothing worked (or I just didn't understand), I'm desperate. Any help would be appreciated. Thanks in advance!
You can sort maps only by the Key element. That's the point in maps.
the normal sort function is not suitable for me.

Yes, it is. Just the normal comparison predicate isn't. Sort has an interface
1
2
template<typename RAI, typename Strict_Weak_Ordering>
  sort(RAI first, RAI beyond, Stric_Weark_Ordering compare);

(where RAI is a Random Access Iterator). Provide a function
1
2
3
4
bool comp_hungarian_strings(const std::string& s1, const std::string& s2)
{
  // return true if s1 comes before s2, false otherwise (i.e., "<" semantics, not "<=" semantics!)
}

now you can use std::sort with all containers that provide a RAI.

Hope that helps.
Last edited on
Thank you exception, you helped a lot. I'll try to make the code. But how can I use that sort function? This way?:

sort(words.begin(), words.end(), comp_hungarian_strings);
Last edited on
You answered your own question...
Thank you exception, again, the program is working and you have put some light in my mind about this topic.
Topic archived. No new replies allowed.