I have an Arraylist
of MyObject
which have a date property busDt
but it is as a String
.
I want to sort my Arraylist
on the date.
I wrote the following code:
Collections.sort(myObjectList, new Comparator<MyObject>() {
public int compare(MyObject o1, MyObject o2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(o1.getBusDt()).compareTo(sdf.parse(o2.getBusDt()));
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
});
Is my code correct ? Any inputs would be a great help!
Thanks for reading!