Sorting unordered map by its value

How to sort unordered map by its values.I have tried by using sort function with help of lambda function,But I am getting error while trying:
1
2
3
sort(names.begin(),names.end(),[](const unordered_map<string,int>::value_type &left, const unordered_map<string,int>::value_type &right){
    return (left.second>right.second);
});


I have also tried:
1
2
3
sort(names.begin(),names.end(),[](const pair<string,int> &left, const pair <string,int> &right){
    return (left.second>right.second);
});

but still getting error.Any help will be appreciated.Thanks!
Last edited on
std::sort requires its first two arguments to be random access iterators
see
http://www.cplusplus.com/reference/algorithm/sort/
http://en.cppreference.com/w/cpp/algorithm/sort

The iterators you're providing, unordered_map<>::iterator, are merely forward iterators:
http://www.cplusplus.com/reference/unordered_map/unordered_map/
http://en.cppreference.com/w/cpp/container/unordered_map

You will need a different container, a vector, or perhaps multimap<int,string>
I think the mistake is in trying to apply a sort order on the unorderd_map container.
Get a sorted (on mapped_type) snapshot view of an associative container or an unordered associative container:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <functional>
#include <set>
#include <string>
#include <unordered_map>
#include <map>
#include <iostream>

template < typename A, typename B >
    using wrapped_pair = std::pair< std::reference_wrapper<A>, std::reference_wrapper<B> > ;

struct cmp_wrapped_pair
{
    template < typename A, typename B > bool operator() ( const A& a, const B& b ) const
    {
        return a.first.get() < b.first.get() ||
               ( !( b.first.get() < a.first.get() ) && a.second.get() < b.second.get() ) ;
    }
};

template < typename MAP_TYPE >
auto sorted_snapshot_of( const MAP_TYPE& map ) // auto: function return type deduction is C++14
{
    using KEY = typename MAP_TYPE::key_type ;
    using VALUE = typename MAP_TYPE::mapped_type ;
    std::multiset< wrapped_pair< const VALUE, const KEY >, cmp_wrapped_pair > snapshot ;

    for( auto& pair : map ) snapshot.emplace( pair.second, pair.first ) ;
    return snapshot ;
}

int main()
{
    const std::unordered_map< std::string, int > unordered_map
    {
        { "zero", 0 }, { "one", 1 }, { "two", 2 }, { "three", 3 }, { "four", 4 },
        { "nihil", 0 }, { "unus", 1 }, { "duo", 2 }, { "tres", 3 }, { "quattuor", 4 },
        { "sunya", 0 }, { "eka", 1 }, { "dvi", 2 }, { "tri", 3 }, { "chatur", 4 }
    };

    // print a sorted (values, then keys) snapshot of the map
    for( const auto& pair : sorted_snapshot_of(unordered_map) )
        std::cout << pair.first.get() << " " << pair.second.get() << '\n' ;
}

http://coliru.stacked-crooked.com/a/987de9b3bb693bc5
http://rextester.com/RQNN91138 (C++11)
Topic archived. No new replies allowed.