Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. sort a map by Value

sort a map by Value

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NajaR12
    wrote on last edited by
    #1

    Hi all, Can anybody tell me how to sort a map based on the values. e.g. Map[key1] = box3; Map[key2] = box1; Map[key3] = box4; Map[key4] = box2; i want result like, Map[key2] = box1; Map[key4] = box2; Map[key1] = box3; Map[key3] = box4; thanks in advance, RKR

    C R C 3 Replies Last reply
    0
    • N NajaR12

      Hi all, Can anybody tell me how to sort a map based on the values. e.g. Map[key1] = box3; Map[key2] = box1; Map[key3] = box4; Map[key4] = box2; i want result like, Map[key2] = box1; Map[key4] = box2; Map[key1] = box3; Map[key3] = box4; thanks in advance, RKR

      C Offline
      C Offline
      Chandrasekharan P
      wrote on last edited by
      #2

      AFAIK, You can sort maps only by the Key element. I think that is the the point of having maps. Your scenario might work out some way. but what if 2 different keys has the same values? how will you sort it?

      Every new day is another chance to change your life.

      1 Reply Last reply
      0
      • N NajaR12

        Hi all, Can anybody tell me how to sort a map based on the values. e.g. Map[key1] = box3; Map[key2] = box1; Map[key3] = box4; Map[key4] = box2; i want result like, Map[key2] = box1; Map[key4] = box2; Map[key1] = box3; Map[key3] = box4; thanks in advance, RKR

        R Offline
        R Offline
        Roger Stoltz
        wrote on last edited by
        #3

        NajaR12 wrote:

        Can anybody tell me how to sort a map based on the values.

        Why do you want to do that? Your desire suggests that you have misunderstood the purpose of a map. The key is supposed to be unique and the value is identified by its corresponding key so you may write code such as in order to retrieve the "box" associated with the key:

        Box& box = Map[key];

        However, if your "boxes" are unique you may use them as keys, e.g.:

        Map[box1] = key2;
        Map[box2] = key4;
        Map[box3] = key1;
        Map[box4] = key3;

        Consider using a std::list that is entry-sorted.

        "It's supposed to be hard, otherwise anybody could do it!" - selfquote
        "High speed never compensates for wrong direction!" - unknown

        1 Reply Last reply
        0
        • N NajaR12

          Hi all, Can anybody tell me how to sort a map based on the values. e.g. Map[key1] = box3; Map[key2] = box1; Map[key3] = box4; Map[key4] = box2; i want result like, Map[key2] = box1; Map[key4] = box2; Map[key1] = box3; Map[key3] = box4; thanks in advance, RKR

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          a map isn't really the right container for this. if you have pairs that need to be sorted, why not put them into a vector/list of std::pair<key,box> and sort that on box ?

          image processing toolkits | batch image processing

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups