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”.
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.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
posted 2 months ago
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.
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?
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