-2

So I want to be able to sort this map into a TreeMap or LinkedHashmap by comparing by a value in the CustomObject i.e compare via object.getSomeValue().

So the object with the highest value will be obviously first in the LinkedHashmap

3
  • What did you try already? What did your own research result in? StackOverflow is supposed to be a last resort when everything else fails, this is not a coding service. Commented Mar 7, 2018 at 9:36
  • Welcome to Stack Overflow! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? and What topics can I ask about here?. From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." Commented Mar 7, 2018 at 9:37
  • you can write your own comparator for it Commented Mar 7, 2018 at 9:38

1 Answer 1

0

You can't do it in reasonable manner, but if you really want it what you can do is:

List<EntrySet<UUID, CustomObject>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, (e1, e2) -> /*comparision here*/);
Map<UUID, CustomObject> sortedMap = new LinkedHashMap<>();
for(Entry<UUID, CustomObject> entry: list){
   sortedMap.put(entry.getKey(), entry.getValue());
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.