Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:01:01+00:00 2026-06-10T19:01:01+00:00

How to sort boost::unordered_map by value and return only keys in that order ?

  • 0

How to sort boost::unordered_map by value and return only keys in that order ?
I have map like boost::unordered_map and I need I need ony list of enums sorted by int values in asc/desc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:01:03+00:00Added an answer on June 10, 2026 at 7:01 pm

    An unordered_map is, as the name implies, inherently not sorted or sortable in-place. You could insert the value pairs into a set that is sorted on the value and get the keys from there (using Boost.Range to make this stuff easier). I use a std::set<T*> to not pay the cost of copying the pair objects.

    #include <iostream>
    #include <set>
    #include <unordered_map>
    #include <boost/range/adaptor/map.hpp>
    #include <boost/range/adaptor/indirected.hpp>
    #include <boost/range/adaptor/transformed.hpp>
    
    struct compare_second{
      template<class Pair>
      bool operator()(Pair* lhs, Pair* rhs) const{
        return lhs->second < rhs->second;
      }
    };
    
    template<class T>
    struct make_pointer{
      typedef T* result_type;
      T* operator()(T& v) const{ return &v; }
    };
    
    int main(){
      using namespace boost::adaptors;
      std::unordered_map<int, int> m{{0,4},{1,3},{2,2},{3,1},{4,0}};
      typedef std::unordered_map<int,int>::value_type pair_type;
      auto p = m | transformed(make_pointer<pair_type>());
      std::set<pair_type*, compare_second> value_ordered(p.begin(), p.end());
      for(auto x : value_ordered | indirected | map_keys)
        std::cout << x << " ";
    }
    

    Live example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found that boost::signals2 uses sort of a lazy deletion of connected slots,
I would like to to sort the edge List of boost::graph defined as followed:
...@Sort bit) AS SELECT .............. and I want to Order only if Sort =
The problem I'm having is that I need to sort a whole bunch of
Sort of a methods/best practices question here that I am sure has been addressed,
Insertion sort requires insertion of an element in sorted order by shifting the elements
how to sort very large numbers like 03 10103538 2222 1233 6160 0142 03
I need to sort a 2 dimensional array of doubles on multiple columns using
I have a MultiIndex container of boost::shared_ptrs to members of class Host . These
I am attempting to write a interpreter that will turn a string like: vector(random(0,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.