I have an ArrayList which contains dates with a format (Satuday,4 Februray 2012). How can I sort this ArrayList ?
4 Answers
If you have any special requirements while sorting, so you may do it by providing your own Comparator. For example:
//your List
ArrayList<Date> d = new ArrayList<Date>();
//Sorting
Collections.sort(d, new Comparator<Date>() {
@Override
public int compare(Date lhs, Date rhs) {
if (lhs.getTime() < rhs.getTime())
return -1;
else if (lhs.getTime() == rhs.getTime())
return 0;
else
return 1;
}
});
The key element is that you are converting your Date object into milliseconds (using getTime()) for comparison.
1 Comment
titogeo
Else just use Collections.sort(<Your Array List>); as @Aerrow said. Dates are comparable.
java.util.Dateobjects do not have a format by themselves.