I have an array list with times. I want to sort it in time Ascending order. I used this but it does not output what I want.
List<String> l = new ArrayList<>();
l.add("12:20 PM");
l.add("12:32 PM");
l.add("12:30 PM");
l.add("12:10 PM");
l.add("12:08 PM");
l.add("12:00 PM");
Collections.sort(l, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
try {
return new SimpleDateFormat("h:mm a").parse(o1).compareTo(new SimpleDateFormat("hh:mm a").parse(o2));
} catch (ParseException e) {
return 0;
}
}
});
System.out.println(l);
I need:
12:00 PM
12:08 PM
12:10 PM...
Likewise