• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Sorting a HashMap Based on a Value within a Class  RSS feed

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a LinkedHashMap ('events') whose key is a string and whose value is a class. Within that class is a date. Is there some way to sort 'events' based on the class date? TIA

Here is the class that is the value of the hashmap:

Here is how I generate the hashmap:
 
Marshal
Posts: 63795
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. But somebody asked a very similar question about two weeks ago. Have a look for it please. Try here.
You can't sort a Map, but you can sort a List, and you would probably want a Comparator to tell you which entries count as larger or smaller. Have a look at this part of the Java™ Tutorails, which tellsyou about ordering objects by “size”.
 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Ritter wrote:I have a LinkedHashMap ('events') whose key is a string and whose value is a class. Within that class is a date. Is there some way to sort 'events' based on the class date?


Yes, but methinks you're trying to do two things with the same collection - in this case, a Hashmap - and that usually ends up in tears.

You're also telling us how you're trying to do something, rather than what you want to do.

So...what DO you want to do? And why isn't your Hashmap working out? And why did you choose a HashMap to begin with?

Hashmaps are great when random retrieval time is vitally important, or specifically for entry-ordered collections, but not otherwise. Lists or Maps are usually better, but they may not solve your problem either.

So...my suggestion is to explain the problem you're having to us in English, NOT in "Java-ese".

Then we'll be in a much better position to advise.

Winston
 
Sam Ritter
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. My intention was not to tell anyone how I want to do something but rather explain what I have. I am generating a collection of information constituting an event as posted. That collection of events is associated with an ID. I choose a hashmap because it was the only way I know of to connect a key (the ID) with its associated event information. I need to list those IDs in order of the date associated with its respective event. Based on Richie's suggestion, I'm thinking I need to create a list of IDs and respective dates then sort that list. With that I can rearrange the linkedhashmap to get the right order. However, since I am extracting the event information from a JSON object, I'm also looking at sorting that based on the date then creating the linkedhashmap.
 
Sheriff
Posts: 13375
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the right tool for the job. Check out TreeMap instead of HashMap. In particular, read the documentation for the constructor that takes a Comparator as its argument.
https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
 
Campbell Ritchie
Marshal
Posts: 63795
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the date was associated with the “V”s in the map, whereas a tree map sort by the “K”s, doesn't it?
 
Sam Ritter
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After some reading I've concluded it will be easier to solve my problem by sorting the JSONArray before creating the hashmap.
 
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Ritter wrote:After some reading I've concluded it will be easier to solve my problem by sorting the JSONArray before creating the hashmap.


Wait wait, stop jumping from idea to idea without any rationale behind that.

And why you need to sort an actual data structure where all things are stored in the first place?

Why not to obtain its keys upon the need, sort them, and retrieve values from Map in a sequential fashion based on the keys you just sorted - that way you don't fiddle with the main data structure, you simply leave it as it is. Another thing, why you have LinkedHashMap? Is it the right implementation for your task?
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I looked back to the code, it seems I have to agree with the guys. So much code written there, but it seems we aren't clear what is the task about, so just purely discussing language's syntax.
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!