How Can I Sort a Multimap by Value in C++?
Last Updated :
20 Oct, 2024
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;
}
Output20: 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;
}
Output20: 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;
}
Output20: 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)
Similar Reads
How to Create a Multimap of Vectors in C++? In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is not required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of vectors in C++. For Example, Input:myPa
2 min read
How to Store Vectors as Keys in a Multimap in C++? In C++, the std::multimap is a container that stores elements in a key-value pair, whereas std::vector is a sequence container that stores elements in contiguous memory. In this article, we will learn how to store vectors as keys in a multimap in C++. Example: Input:myVector ={1,2,3};myVector ={4,5,
2 min read
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 - u
4 min read
How to Create a Multimap of Arrays in C++? In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is NOT required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of arrays in C++ STL. Example Input: myArr
2 min read
How to Sort a Vector in a Map in C++? In C++, we can create a map container where the values associated with keys is a vector. In this article, we will learn how to sort a vector within a map in C++. Example Input: myMap = { {3, {9, 7, 3}}, {5, {4, 2, 8, 1, 6}}, {8, {1, 2, 5, 8}} }; Output: Map: Key: 3, Sorted Vector: [3 7 9 ] Key: 5, S
2 min read