I have a custom object whose structure is
public String name;
public int type;
public String createdOn;
createdOn
is date and its format is : 2011-12-16T23:27:27+0000
I have stored multiple objects of this type in ArrayList
and now I want to sort them according to there creation date and time.
I have tried using Collections.sort(....)
, but no suitable result.
Doing some R&D after getting answers, I solved the sorting thing, here is the code to sort array list by date.
Collections.sort(arrlst,new Comparator<T>() {
public int compare(T lhs, T rhs) {
try {
SimpleDateFormat dateFormatlhs = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date convertedDatelhs = dateFormatlhs.parse(lhs.feedCreatedTime);
Calendar calendarlhs = Calendar.getInstance();
calendarlhs.setTime(convertedDatelhs);
SimpleDateFormat dateFormatrhs = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date convertedDaterhs = dateFormatrhs.parse(rhs.feedCreatedTime);
Calendar calendarrhs = Calendar.getInstance();
calendarrhs.setTime(convertedDaterhs);
if(calendarlhs.getTimeInMillis() > calendarrhs.getTimeInMillis())
{
return -1;
}
else
{
return 1;
}
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
});