0

I have a Map which contains Strings and objects and I would like to sort based on an object attribute.

public class Foo{
     public long timestamp,
     public String name;
     public Foo(long timestamp,String name){
          this.timestamp = timestamp;
          this.name = name;
    }
}
public static main(){
    Map<String,Foo> map = new HashMap<String,Foo>(0);
    for(int i; i<1000;i++){
          //getting the key and filling the Foo objects
          Foo foo = new Foo(datetime,name);
          String key = i;
          map.put(key,Foo);
    }
  //sorting
   Map<String,Foo> sortedMap = sortData(map);

}
public Map<String,Foo> sortData(Map<String map,Foo> map){
           //SORTING HERE THE MAP BASED ON THE TIMESTAMP
}

The sorting is based on the timestamp attribute of the Foo Object. How can I do it?

BBR
  • 23
  • 4
  • 1
    A HashMap is not a sortedMap. First you need to change your map for a TreeMap. Then try to use a custom comparator to sort your map. – lohnns May 01 '18 at 17:53
  • see https://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values – lohnns May 01 '18 at 17:57

0 Answers0