5
I have the following method to sort a ArrayList
String of Hours:
public static void ordenaHoras() {
ArrayList<String> horasList = new ArrayList<String>();
horasList.add("23:45");
horasList.add("11:13");
horasList.add("15:33");
horasList.add("12:27");
horasList.add("15:24");
Collections.sort(horasList, new Comparator<String>() {
private SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
@Override
public int compare(String o1, String o2) {
int result = -1;
try {
result = sdf.parse(o1).compareTo(sdf.parse(o2));
} catch (ParseException ex) {
ex.printStackTrace();
}
return result;
}
});
for (String hora: horasList) {
System.out.println(hora);
}
}
My problem is that the values of hours starting at number 12 always get wrongly first!!! The output of the execution of the top method is:
12:27
11:13
15:24
15:33
23:45