Sorting a Map by value in C++ STL
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. Below is the various method to achieve this:
Method 1 – using the vector of pairs The idea is to copy all contents from the map to the corresponding vector of pairs and sort the vector of pairs according to second value using the lambda function given below:
Below is the implementation of the above approach:
Time Complexity: O(n*log(n)), where n is length of given map
Auxiliary Space: O(n)
Method 2 – using the set of pairs The idea is to insert all the (key-value) pairs from the map into a set of pairs that can be constructed using a comparator function that orders the pairs according to the second value.
Below is the implementation of the above approach:
Time Complexity: O(n*log(n)), where n is the length of the given map
Auxiliary Space: O(n)
Method 3 – using multimap Multimap is similar to a map with an addition that multiple elements can have the same keys. Rather than each element is unique, the key-value and mapped value pair have to be unique in this case. The idea is to insert all pairs from the given map into multimap using the originals map’s value as a key in the multimap and original maps key value as a value in the multimap.
Below is the implementation of the above approach:
Time Complexity: O(n*log(n)), where n is the length of the given map
Auxiliary Space: O(n)
2 Ways to Sort Map by Values in C++
Summary: In this programming tutorial, we will learn different ways to sort a Map by values in C++.
Map in C++ is an associative container that stores key-value pairs in an ordered sequence of keys.
Although we cannot directly have elements sorted with respect to values, there are some indirect ways using which we can sort a Map by values in C++.
Method 1: Using std::vector
Output:
Method 2: Using std::multimap
Another way we can achieve this is by flipping the key-value pairs (i.e. using keys as values and their corresponding values as keys) and sorting on the basis of keys.
But it will only work if the map has distinct values.
To solve this issue, we can use a multimap.
A multimap in C++ is similar to a map, but it can store elements with duplicate keys.
Output:
These were the two ways using which we can easily sort a map data structure by values in C++.
How to Sort a Map by Value in C++
In this article we will discuss how to sort a map by value in both ascending and descending order.
A Map store the elements in the sorted order of keys.
For example, we have a map of words and its frequency count as key – value pair i.e.
Now lets insert following elements in to this map,
Map internally stores the above elements in sorted order of keys i.e.
Therefore, iterating over a map will give pair elements in above order. Now what if want to display items of a map in sorted order of keys. For example for above map we want to display the items in descending order of frequency count i.e.
Frequently Asked:
For that we need to sort the entries in map by value.
Sorting a Map by Ascending Order of Value
Map contains pairs of key & value. Where first field of std::pair represents the key and second field represents the value.
Therefore, we can sort the pairs by keeping them in a set and using a comparison logic that compares them with their second field instead of first one.
In the below example we will use a comparator as lambda function that compares the 2 pais using teir second field i.e.
We will create a set of pair that will use the above lambda function as compare function and keep the added pairs in sorted order of second field of std::pair.
Then we will copy the elements from map to set and it will store them in sorted order of values.
Sort Map by Value in C++
This article will demonstrate multiple methods about how to sort the map by values in C++.
Use std::vector and std::sort Algorithm to Sort Map Elements by Value in C++
Please enable JavaScript
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. In this case, we are declaring a sample std::map object with integer strings as keys and regular strings as values. The problem is to sort these elements by the values of the strings.
We can’t directly use the std::sort algorithm on the std::map structure, so we have to initialize another object that can be sorted. Thus, the declaration of the std::vector contains the pairs of the same type. We construct vector elements using the for loop and emplace_back method. Once the loop is executed, the vector is ready to be passed to the std::sort algorithm. Notice that we specify the lambda expression to define the element comparison function, and it only compares the second members. Finally, we can output the std::vector elements as a sorted map representation.
Use std::map and std::map::emplace to Sort Map Elements by Value in C++
The previous solution did not deal with the std::map object itself and used an external structure for sorting. In this case, we implement a solution to store the value-sorted elements in another std::map object. This can be achieved using the built-in map function — emplace . Namely, we declare another map object and construct its elements using the emplace method, but we also pass the reversed key-value pairs. As a result, the map container automatically sorts elements by keys: the values in the previous map object. Next, we can use the sorted map object for other operations that may be needed in the following code blocks without worrying that it’s stored in a different object.