Open In App

How Can I Sort a Multimap by Value in C++?

Last Updated : 20 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, std::multimap stores elements as key-value pairs sorted by keys and there is no way to modify it to sort the elements by value but we can create a workaround to achieve it by storing its elements in some other containers that allow us to sort the elements by value.

In this article, we will learn how to sort the std::multimap elements by value in C++.

Examples

Input: mm = { {10, 2}, {20, 1}, {30, 4}, {40, 3} }
Output: [20: 1] [10: 2] [40: 3] [30: 4]
Explanation: The multimap's elements are sorted in increasing order based on value.

Input: mm = { {11, 9}, {20, 7}, {30, 12}, {40, 11} }
Output: [30: 12] [40: 11] [11: 9] [20: 7]
Explanation: The multimap's elements are sorted in decreasing order based on value.

Following are the 3 different methods to sort the std::multimap elements by value in C++:

Using Vector of Pairs

We can sort the std::multimap elements on the basis of the value by first copying all the elements into the vector of pair and then sorting them by second parameter using std::sort() method with a custom comparator.

Code Implementation

C++
// C++ program to sort a multimap by its value
// using vector of pairs
#include <bits/stdc++.h>
using namespace std;

bool comp(pair<int, int>& a, pair<int, int>& b) {
    return a.second < b.second;
}

int main() {
    multimap<int, int> mm = {{10, 2}, {20, 1}, 
                             {30, 4}, {40, 3}};
                             
    // Vector of pairs created using above multimap
    vector<pair<int, int>> v(mm.begin(), mm.end());

    // Sort the vector based on the second parameter
    // using a lambda function as comparator
    sort(v.begin(), v.end(), comp);
    for (auto i : v)
        cout << i.first << ": " << i.second << endl;
    return 0;
}

Output
20: 1
10: 2
40: 3
30: 4

Time Complexity: O(n * log n) where n is the number of elements in the multimap.
Auxiliary Space: O(n)

Using Set of Pairs

In above method, we have to manually sort the data after insertion. But we can avoid this extra step by choosing an associative container such as std::set that already sorts the given data in some specified order (by default increasing order) using custom set comparator.

Code Implementation

C++
// C++ program to sort a multimap by its value
// using set with a custom comparator
#include <bits/stdc++.h>
using namespace std;

bool comp(const pair<int, int> &a, const
          pair<int, int> &b) {
    return a.second < b.second;
}

int main() {
    multimap<int, int> mm = {{10, 2}, {20, 1},
                             {30, 4}, {40, 3}};

    // Create a set with custom comparator to sort
  	// by value
    set<pair<int, int>, decltype(&comp)> s(mm.begin(),
                                     mm.end(), &comp);
    for (const auto i : s) {
        cout << i.first << ": " << i.second << endl;
    }
    return 0;
}

Output
20: 1
10: 2
40: 3
30: 4

Time Complexity: O(n * log n) where n is the number of elements in the multimap.
Auxiliary Space: O(n)

By Swapping Keys and Values

We know that std::multimap are sorted on the basis of keys, so if we insert all the pairs of original multimap into another multimap by swapping the keys of each element by its value, then the new multimap will be sorted on the basis of value of original multimap.

Code Implementation

C++
// C++ program to sort the key-value of multimap
// on the basis of value using another multimap
#include <bits/stdc++.h>
using namespace std;

int main() {
    map<int, int> mm1 = {{10, 2}, {20, 1}, 
                        {30, 4}, {40, 3}};

    // Declare a new multimap
    multimap<int, int> mm2;

    // Insert every (key-value) pairs from original 
    // multimap mm to another multimap mm1 as
  	// (value-key) pairs
    for (auto i : mm1)
        mm2.insert({i.second, i.first});

    for (auto i : mm2)
        cout << i.second << ": " << i.first << endl;
    return 0;
}

Output
20: 1
10: 2
40: 3
30: 4

Time Complexity: O(n * log n) where n is the number of elements in the multimap.
Auxiliary Space: O(n)


Practice Tags :

Similar Reads