Loop through the unordered_map and add each key to the vector and then sort the vector after the loop. Or you could simply use std::map instead of std::unordered_map and it would always be sorted automatically.
Hi, i want sort this unordered_map in alphabetical order with the function sort of the library algorithm using vectors. Look the my code! Help me!
Sort requires a container to have a random-access-operator, unordered map does not fulfill that condition so you can't sort it with sort.
You could, like Peter87 said, use std::map instead of std::unordered_map, then the keys will allways be sorted.
The standard sort condition is allways less-than, so the smallest element will be at the beginning.
If you want to have it sorted differently use the 3rd template parameter to give it a compare predicate, for example a function-object from the c++ standard.
The c++ standard provides a list of function-objects for comparison: http://www.cplusplus.com/reference/functional/
There is an example in the previous post so I'm not sure what you are asking Ozzy69. Unordered containers are named that way for a reason. They are not supposed to be sorted. They are typically implemented using hashing so that accessing and storing items is faster.