0

How can I sort the map using the data from a value Example: Map<String, List> is my input. I need to sort and get the order based on timestamp

Example:

class SomeObject{
 String var1; 
Timestamp var2; 
String var3; // getters and setters }

INPUT:

Entry 1:

{ Key1 = 123 [ someObject [ var1=4 var2=08.02.2020 13:00:00, var3=6 ] ] }

Entry 2:

{ Key1 = 456 [ someObject [ var1=2 var2=08.02.2020 15:00:00 var3=1 ] ] }

Entry 3:

{ Key1 = 789 [ someObject [ var1=1 var2=08.02.2020 03:00:00 var3=3 ] ] }

OUTPUT after sorting according to var2:

Entry 1:

{ Key1 = 789 [ someObject [ var1=1 var2=08.02.2020 03:00:00 var3=3 ] ] }

Entry 2:

{ Key1 = 123 [ someObject [ var1=4 var2=08.02.2020 13:00:00 var3=6 ] ] }

Entry 3:

{ Key1 = 456 [ someObject [ var1=2 var2=08.02.2020 15:00:00 var3=1 ] ] }

There are 3 entries in Map with format Map<String, List> I need to sort this Map collection with var2 values

Before keys are in: 123,456,789

After sorting keys are in: 789,123,456

  • 1
    I think you are missing lot of data that will help getting the answer to the question, Please look at [How to ask](https://stackoverflow.com/help/how-to-ask) . Also i believe this is a duplicate of https://stackoverflow.com/questions/8119366/sorting-hashmap-by-values. Please check if this helps you with what you are trying to achieve – fatcook Mar 09 '21 at 09:29
  • Get yourself a Comparator which uses timestamp for ordering. Think about what happens if you have two objects with the same var2 - what is the fallback to use to compare in that case? (see [stable sort](https://stackoverflow.com/questions/32711035/stable-sort-do-we-really-need-it)). – Mr R Mar 09 '21 at 09:59
  • Why do you have a `Map`? Shouldn't it be `Map`? Also, after sorting, do you want the sorted list of keys, of values, or of key-value pairs? – tobias_k Mar 09 '21 at 10:10

0 Answers0