2 Ways to Sort Map by Values in C++

C++ Programs

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

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

/* method to compare two pairs based on the second parameter,
it is used as a comparator function to sort a vector of pairs */
bool sortByVal(const pair<int, string> &a, const pair<int, string> &b){
    return a.second < b.second;
}

int main() {
    map<int, string> studentsMap = {{1, "Jacob" },
                                    {2, "Andrew"},
                                    {3, "Simon" }};
    //output map before sorting 
    cout << "Before Sorting: \n";
    for(auto m: studentsMap){
        cout << "{" << m.first << ": " << m.second << "} \n";
    }
    
    //copy the elements of the map to the vector as pairs
    vector<pair<int, string>> sVector;
    for(auto m: studentsMap){
        sVector.push_back(make_pair(m.first, m.second));
    }
    
    //sort the vector using the sort() method
    sort(sVector.begin(), sVector.end(), sortByVal); 
    
    //output the sorted vector
    cout << "After Sorting by Value: \n";
    for(auto v: sVector){
        cout << "{" << v.first << ": " << v.second << "} \n";
    }
    
    return 0;
}

Output:

Before Sorting:
{1: Jacob}
{2: Andrew}
{3: Simon}
After Sorting by Value:
{2: Andrew}
{1: Jacob}
{3: Simon}

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.

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<int, string> studentsMap = {{1, "Jacob" },
                                    {2, "Andrew"},
                                    {3, "Simon" }};
    //output map before sorting 
    cout << "Before Sorting: \n";
    for(auto m: studentsMap){
        cout << "{" << m.first << ": " << m.second << "} \n";
    }
    
    /* copy elements to multimap by fliping
    the key-value pairs */
    multimap<string, int> multiMap;
    
    for(auto m: studentsMap){
        multiMap.insert(make_pair(m.second, m.first));
    }
    
    //output the multimap
    cout << "After Sorting by Value: \n";
    for(auto el: multiMap){
        cout << "{" << el.first << ": " << el.second << "} \n";
    }
    
    return 0;
}

Output:

Before Sorting:
{1: Jacob}
{2: Andrew}
{3: Simon}
After Sorting by Value:
{Andrew: 2}
{Jacob: 1}
{Simon: 3}

These were the two ways using which we can easily sort a map data structure by values in C++.

Leave a Reply

Your email address will not be published. Required fields are marked *