0

So I have several objects in a HashMap: private HashMap <String,Object> hmap;

what I want to do is sort this objects through a variable of the objects (a double). How should I do this? I think I'm required to use comparators, but I don't know exactly how to work with them. Should I send the objects to an array?

danielnovais92
  • 633
  • 3
  • 8
  • 19

1 Answers1

2

I guess you will need to do sorting in a separate List. You will need something like below:

List<Object> values = new ArrayList<Object>();
values.addAll(hmap.values());
Collections.sort( values, new Comparator<Object>(){ 
 public int compare(Object o1,
                   Object o2){
    //do specific comparison here
 });
Hakan Serce
  • 11,198
  • 3
  • 29
  • 48