Sort Vector of Maps by Map Value

I'm trying to sort a vector of maps by its stored value (not key) in descending order. Each index will have a series of addresses stored there, along with a key to keep track of which address was recently placed at that particular index location in the vector. I know that the sorting usually takes place based on the key and not the value. The reason why I cannot sort based on the key is because the keys are addresses and the values are the number of times the address has been encountered. As a result, I use the key to find the address, and then update its value with the current count. In the end, I would like to order and print out the addresses from the most frequently encountered to the less frequently encountered. I tried making my own "sort" function with a value comparator. This is the code I have so far:

1
2
3
4
5
6
7
8
9
void sortIndex(uint32_t index, vector<map<uint32_t, int> >& table)
{
	sort(table.at(index).begin(), table.at(index).end(), &maxValue);
}

bool maxValue(pair<uint32_t, int> i, pair<uint32_t, int> j)
{
	return i.second > j.second;
}


Unfortunately I keep getting the following errors from VS2013 "algorithm.h" file:

Compiler Error C2784: 'declaration' : could not deduce template argument for 'type' from 'type'. The compiler cannot determine a template argument from the supplied function arguments.

Compiler Error C2676: binary 'operator' : 'type' does not define this operator or a conversion to a type acceptable to the predefined operator. To use the operator, you must overload it for the specified type or define a conversion to a type for which the operator is defined.

Compiler Error C2780: 'declaration' : expects number1 arguments - number2 provided. A function template has too few arguments.

If the errors were in my code directly, I could solve them since I wrote it and would just modify it. But, the errors are from one of the header files, so I cannot figure out the exact source of my problem to correct it. My only assumption is that the issues lie within my sort statements. I've researched how to implement the value sort and fix my compiling issues with no luck. Is there a way I can sort a map based on its value?

Thanks!
You cannot sort a map (map is a tree) because maps use bidirectional iterators rather than random_access iterators which is what std::sort needs in order to sort a range. What you can do is to copy all the elements into a vector, then you can sort the vector based on the values used in the map; like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
using mt = map<uint32_t, int>;

vector<uint32_t> sortIndex(int index, vector<mt> &table) {
    vector<uint32_t> sorted;
    
    // copy each key into the vector
    for_each(table[index].begin(), table[index].end(), [&](mt::value_type &obj) {
        sorted.emplace_back(obj.first);
    });
    
    // sort the vector based on the value of each key
    sort(sorted.begin(), sorted.end(), [&](const uint32_t &k1, const uint32_t &k2)-> bool {
        return table[index][k1] > table[index][k2];
    });
    
    return sorted;
}

int main() {
    vector<mt> table = {{{0xffffff, 2}, {0x45a1fd, 12}, {0xe9fa916, 4}}};
    for (auto v : sortIndex(0, table)) {
        cout << hex << v << ' ' << endl;
    }
    return 0;
}
Last edited on
Topic archived. No new replies allowed.