• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Sorting Map with values with no particular order

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a Map<Object, String> that I need to sort with values in no particular order What I mean is currently the Map has the values like "A","B","C",D",E" for respective keys. I need to sort it as "C","D","E","A","B". I know we can write Comparator when there is some logic behind sorting. But I am not sure how to sort the Map this way.

Thanks in advance,
Ankit
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use TreeMap if you write a Comparator or the object itself has implemented Comparable interface.
But "no particular order" means random order? why CDEAB but not CDEBA ?
 
Ankit Chandrawat
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no reason for this. It just has to be like this. That is why I am not able to understand how to work it around ?
 
Marshal
Posts: 80282
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is not sorting. That is the opposite of sorting.
Maps do not support a recognisable ordering as a default. There are two kinds of Map which do,
  • The composite Map (well, I call it that), which contains both a Map and a List, whose Iterator returns K‑V pairs in order of first insertion.
  • The Sorted Map, whose Iterator returns pairs in ascending order of the value of the “K” (I think). A Map which iterates A‑B‑C‑D‑E sounds like a sorted Map.
  • I am not aware of any way to un‑sort a Map.
    You can try putting all the elements into an ordinary Map (e.g. HashMap), but you will probably get things sorted by hash code in reverse bit order, so youmight still get A‑B‑C‑D‑E.
    You can try loading all the pairs into a List and using the methods of the Collections class to randomise the order of that List. Then add all the elements back into a combined Map and List.
     
    Campbell Ritchie
    Marshal
    Posts: 80282
    432
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ankit Chandrawat wrote:There is no reason for this. It just has to be like this. . . .

    There are all sorts of jokes which end like that. Mostly bad ones.

    Ask what the reason is.
     
    Ankit Chandrawat
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If I had known the reason or any sort of logic behind this I would have worked on writing a Comparator on this. But there is no logic to this and that's why I am baffled.
     
    Ankit Chandrawat
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The thing is, there is an order to it. But there is no logic behind it.
     
    Bartender
    Posts: 4179
    22
    IntelliJ IDE Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ankit Chandrawat wrote:If I had known the reason or any sort of logic behind this I would have worked on writing a Comparator on this. But there is no logic to this and that's why I am baffled.


    If you can't explain how the 'sort' is supposed to happen, then you can't tell the computer how to do it. You have to learn what the requirement means and understand its rules. Once you understand the rules enough that you can tell someone else how it is supposed to work - then you can think about programming it. Until then it is impossible.
     
    Ankit Chandrawat
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The correct word for it is not sorting. Apologies for the confusion. But it has an order and the order is specific. It's more like placing the element at designated places.
     
    Steve Luke
    Bartender
    Posts: 4179
    22
    IntelliJ IDE Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    How about:

    Since there is no logic to their order (so say you), then the only thing left to do is hard code (and hope a value which isn't hard coded doesn't show up).
     
    Campbell Ritchie
    Marshal
    Posts: 80282
    432
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Write down what the order is supposed to be, and you might be able to work out what its logic is. If there is no logic to it, then it is impossible to code.
     
    Yeah, but is it art? What do you think tiny ad?
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic