I want to sort a Hashmap by multiple keys, at the moment i use the following code
Collections.sort(ilv2, new Comparator<HashMap<String,String>>() {
public int compare(HashMap<String, String> arg0, HashMap<String, String> arg1) {
// TODO Auto-generated method stub
Time d1 = Time.valueOf(arg0.get("stamp"));
Time d2 =Time.valueOf(arg1.get("stamp"));
return d1.compareTo(d2);
}
});
Th Hasmap ilv2 is sortet by the Time key “stamp”, but now i want to sort by the key res_id (int) and then by time, like the sql statement “order by res_id, stamp”
Any suggestions?
edit:
solution based on the given answer
Collections.sort(ilv2, new Comparator<HashMap<String,String>>() {
public int compare(HashMap<String, String> arg0, HashMap<String, String> arg1) {
// TODO Auto-generated method stub
int i = 0;
int r1 = Integer.valueOf(arg0.get("resid"));
int r2 = Integer.valueOf(arg1.get("resid"));
i = r1-r2;
if(i==0){
Time d1 = Time.valueOf(arg0.get("stamp"));
Time d2 =Time.valueOf(arg1.get("stamp"));
i = d1.compareTo(d2);
}
return i;
}
});
From my code, something like this:
Read about what compareTo return.