I'm trying to figure out how to sort std::map
by ascending order of value.
My Code :
#include <iostream>
#include <map>
#include <string>
#include <iterator>
void printMapByOrder(std::map<std::string, int> mapOfPlanets)
{
//what should be here?
}
int main() {
std::map<std::string, int> mapOfPlanets;
mapOfPlanets["earth"] = 12;
mapOfPlanets["jupiter"] = 142;
mapOfPlanets["mars"] = 6;
mapOfPlanets["mercury"] = 4;
mapOfPlanets["neptune"] = 49;
mapOfPlanets["pluto"] = 2;
mapOfPlanets["saturn"] = 120;
mapOfPlanets["uranus"] = 51;
mapOfPlanets["venus"] = 12;
printMapByOrder(mapOfPlanets);
}
The Result I want :
pluto : 2
mercury : 4
mars : 6
earth : 12
venus : 12
neptune : 49
uranus : 51
saturn : 120
jupiter : 142
Is this possible to do this with std::map
?