I have an ArrayList
which contains dates with a format (Satuday,4 Februray 2012). How can I sort this ArrayList
?
This is one of the Simplest way to sort,
Collections.sort(<Your Array List>);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With