473,221 Members | 1,967 Online

sorting maps according to their value_type

I'm designing an algorithm with maps that need the elements to be
sorted by value instead of by key. Does anyone have a suggestion on how
to do that?

#include <map>
using namespace std;

int main()
{
// how can I specify a SortingCriterion based on the value so that
// the months are ordered from the one with largest number of day
// to the one with smallest?
map<const char*, int /*, SortingCriterion*/ > months;

months["february"] = 28;
months["march"] = 31;
months["april"] = 30;

}

Thanks & regards
Francesco

Feb 10 '06 #1
5 1573
cesco wrote:
I'm designing an algorithm with maps that need the elements to be
sorted by value instead of by key. Does anyone have a suggestion on how
to do that?

Why? It's sorted by key for quick lookups. If you sort by value, I
suspect you'll find it hard to find what you're looking for.

What about an iterator that traverses the sequence in the specified
order? Or a different container?

What problem do you need to solve such that the months should be day length?

What about a set or pairs (string, int) with sorting criterion on .second?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 10 '06 #2
Thanks for the reply.

The reason why I need to sort them according to the value is that later
on I apply the algorithm for_each (combined, of course, with a function
object) starting from the first element that should have the highest
value in the value_type field and going to the last. I'd like to avoid
a vector of pairs because I'm reusing some code and in that case I may
have to change a lot.

Any other suggestion?

Thanks again
Francesco

Feb 10 '06 #3
cesco wrote:
Thanks for the reply.

The reason why I need to sort them according to the value is that later
on I apply the algorithm for_each (combined, of course, with a function
object) starting from the first element that should have the highest
value in the value_type field and going to the last. I'd like to avoid
a vector of pairs because I'm reusing some code and in that case I may
have to change a lot.

Any other suggestion?

You didn't comment on my iterator idea. You can create your own
iterator that iterates the sequence you require.

In fact, if you use a std::set<std::pair<int, std::string> >, the

Since these are static, you could also create a set that gives you the
ordering you require, but still store the data in the map. Or a vector
of std::map<std::string, int>::const_iterator that gives you the
sequence you require...

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 10 '06 #4
maybe something on the line of:

map<int,float> externallist;

class comparethem
{
public:
map<int,float> *list;
bool operator()(int a,int b){return (*list)[a]<(*list)[b];}
};

void sortit(map<key,value> list)
{
comparethem cmp;
cmp.list=&list;

sort(list.begin(),list.end(),cmp);
//put them into original

}

Feb 10 '06 #5

cesco wrote:
I'm designing an algorithm with maps that need the elements to be
sorted by value instead of by key. Does anyone have a suggestion on how
to do that?

#include <map>
using namespace std;

int main()
{
// how can I specify a SortingCriterion based on the value so that
// the months are ordered from the one with largest number of day
// to the one with smallest?
map<const char*, int /*, SortingCriterion*/ > months;

months["february"] = 28;
months["march"] = 31;
months["april"] = 30;

}

Then just use a multimap with the number of days as the key. If you
want two or more orders / indexes in your map use boost::multi_index