Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 74774
336
  • 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: 74774
    336
    • 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: 74774
    336
    • 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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic