The map
is actually a tree, and is sorted by KEY order. You are printing itr->second
which is the VALUE not the KEY. If you want your key/value pairs sorted by VALUE, use the VALUE as key instead, or store everything in another container (say an array), then sort them.