-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

RyanW17
  • 21
  • 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. – Ben Mar 07 '18 at 09:36
  • Welcome to Stack Overflow! Please take the [tour](/tour), have a look around, and read through the [help center](/help), in particular [How do I ask a good question?](/help/how-to-ask) and [What topics can I ask about here?](/help/on-topic). 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." – Timothy Truckle Mar 07 '18 at 09:37
  • you can write your own comparator for it – Vikas Suryawanshi Mar 07 '18 at 09:38

1 Answers1

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());
}
Yoda
  • 17,363
  • 67
  • 204
  • 344