Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting std::map using value

I need to sort an std::map by value rather than by key. Is there an easy way to do it?

I got one solution from the follwing thread:
std::map sort by data?
Is there a better solution?

map<long, double> testMap; // some code to generate the values in the map.  sort(testMap.begin(), testMap.end());  // is there any function like this to sort the map? 
like image 940
user619237 Avatar asked Feb 20 '11 11:02

user619237


People also ask

Can you sort a map by value in C++?

Use std::vector and std::sort Algorithm to Sort Map Elements by Value in C++ std::map is an associative container that can store key-value pairs with unique keys, and the latter are used to sort the elements in the object automatically.

Can you sort a map based on value?

Solution: The idea is to store the entry set in a list and sort the list on the basis of values. Then fetch values and keys from the list and put them in a new hashmap. Thus, a new hashmap is sorted according to values.

How do you sort a map by key-value?

Step 1: Create a TreeMap in java with a custom comparator. Step 2: Comparator should compare based on values and then based on the keys. Step 3: Put all key-value pairs from the hashmap into the treemap. Step 4: return the treemap.

Is C++ map sorted by key or value?

Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have equal key values. By default, a Map in C++ is sorted in increasing order based on its key.


2 Answers

Even though correct answers have already been posted, I thought I'd add a demo of how you can do this cleanly:

template<typename A, typename B> std::pair<B,A> flip_pair(const std::pair<A,B> &p) {     return std::pair<B,A>(p.second, p.first); }  template<typename A, typename B> std::multimap<B,A> flip_map(const std::map<A,B> &src) {     std::multimap<B,A> dst;     std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()),                     flip_pair<A,B>);     return dst; }  int main(void) {     std::map<int, double> src;      ...          std::multimap<double, int> dst = flip_map(src);     // dst is now sorted by what used to be the value in src! } 

Generic Associative Source (requires C++11)

If you're using an alternate to std::map for the source associative container (such as std::unordered_map), you could code a separate overload, but in the end the action is still the same, so a generalized associative container using variadic templates can be used for either mapping construct:

// flips an associative container of A,B pairs to B,A pairs template<typename A, typename B, template<class,class,class...> class M, class... Args> std::multimap<B,A> flip_map(const M<A,B,Args...> &src) {     std::multimap<B,A> dst;     std::transform(src.begin(), src.end(),                    std::inserter(dst, dst.begin()),                    flip_pair<A,B>);     return dst; } 

This will work for both std::map and std::unordered_map as the source of the flip.

like image 148
Oliver Charlesworth Avatar answered Sep 25 '22 00:09

Oliver Charlesworth


I needed something similar, but the flipped map wouldn't work for me. I just copied out my map (freq below) into a vector of pairs, then sorted the pairs however I wanted.

std::vector<std::pair<int, int>> pairs; for (auto itr = freq.begin(); itr != freq.end(); ++itr)     pairs.push_back(*itr);  sort(pairs.begin(), pairs.end(), [=](std::pair<int, int>& a, std::pair<int, int>& b) {     return a.second < b.second; } ); 
like image 33
NielW Avatar answered Sep 22 '22 00:09

NielW