How to Sort Date in descending order From Arraylist Date in android?
Just add like this in case 1: like this
case 0:
list = DBAdpter.requestUserData(assosiatetoken);
Collections.sort(list, byDate);
for (int i = 0; i < list.size(); i++) {
if (list.get(i).lastModifiedDate != null) {
lv.setAdapter(new MyListAdapter(
getApplicationContext(), list));
}
}
break;
and put this method at end of the your class
static final Comparator<All_Request_data_dto> byDate = new Comparator<All_Request_data_dto>() {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
public int compare(All_Request_data_dto ord1, All_Request_data_dto ord2) {
Date d1 = null;
Date d2 = null;
try {
d1 = sdf.parse(ord1.lastModifiedDate);
d2 = sdf.parse(ord2.lastModifiedDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (d1.getTime() > d2.getTime() ? -1 : 1); //descending
// return (d1.getTime() > d2.getTime() ? 1 : -1); //ascending
}
};
Create Arraylist<Date>
of Date class. And use Collections.sort()
for ascending order.
See sort(List<T> list)
Sorts the specified list into ascending order, according to the natural ordering of its elements.
For Sort it in descending order See Collections.reverseOrder()
Collections.sort(yourList, Collections.reverseOrder());