Reputation: 604
I have a ArrayList with custom objects. Each object has two date entities Call date
and Appointment date
. This information (along with others) are displayed in a list view and everything so far is working fine.
The Call date
is never null and is always valid date from the server. However, the Appointment date
can be null for a few entries. The date format for both Call date
and Appointment date
is the same (Thu Jan 01 00:00:00 GMT 1970)
The sorting should work like this:
The Call date
should be sorted in Descending order of date
Appointment date, if not null should show on top. However, it has to be sorted in the ascending order of the date (e.g.: If there are two appointments one on 19th August and the other on 21st August, the 19th August should show on top and the 21st below it) and all nulls should be at the bottom
Currently, I have got the Call date
and Appointment date
sorted by using two separate comparators. However, the Appointment date
is in descending order instead of the ascending order (21st is showing on top, 19th below it and nulls at the end) as required.
The comparator code is below
public class CallDateComparator implements Comparator< ListModel > {
@Override
public int compare(ListModel o1, ListModel o2) {
if (o2.getCallDate() != null && o1. getCallDate() != null)
return o2.getProspectDate().compareTo(o1.getProspectDate());
else
return 0;
}
}
public class AppointmentDateComparator implements Comparator< ListModel > {
@Override
public int compare(ListModel o1, ListModel o2) {
if (o2.getAppointmentDate() != null && o1.getAppointmentDate() != null)
return o2.getAppointmentDate().compareTo(o1.getAppointmentDate());
else
return 0;
}
}
I sort the ArrayList
by calling
Collections.sort(myList, new CallDateComparator());
Collections.sort(myList, new AppointmentDateComparator());
What needs to be changed above to get the appointment date sorted in ascending order and if it is null, just leave the earlier sorting intact?
Upvotes: 2
Views: 785
Reputation: 907
my approach based on the format on the comments :
basically i'd do a bigger comparator rather than two different ones.
public class CallDateComparator implements Comparator< ListModel > {
@Override
public int compare(ListModel o1, ListModel o2) {
if (o2.getCallDate() != null && o1. getCallDate() != null)
int i = o1.getProspectDate().compareTo(o2.getProspectDate();
switch (i)
case -1: //o1 smaller than o2
return -1;
break;//this might be not necessary at all.
case 0: //both equal, so, same call date.
if(o1.getAppointmentDate() != null && o2.getAppointmentDate() == null)
{
return 1; // make o1 the bigger one.
}
else if(o1.getAppointmentDate() == null && o2.getAppointmentDate() != null)
{
return -1; // make o2 the bigger one.
}
else if(o1.getAppointmentDate() == null && o2.getAppointmentDate() == null)
{
return 0; //both are equally null.
}
else
{
return o1.getAppointmentDate().compareTo(o2.getAppointmentDate()); //compare then
}
break;
case 1: //o1 bigger than o2
return 1;
break;//this might be not necessary at all.
default:
return 0;
break;
else
return 0;
}
}
Upvotes: 1