使用 android 中的两个日期对象对数组列表进行排序

Sort array list with two date objects in android

我有一个带有自定义对象的 ArrayList。每个对象都有两个日期实体 Call dateAppointment date。此信息(连同其他信息)显示在列表视图中,目前一切正常。

Call date 永远不会为空,并且始终是来自服务器的有效日期。但是,Appointment date 对于一些条目可以为空。 Call dateAppointment date 的日期格式相同 (Thu Jan 01 00:00:00 GMT 1970)

排序应该是这样的:

目前,我已经使用两个单独的比较器对 Call dateAppointment date 进行了排序。但是,Appointment date 根据需要按降序排列而不是升序排列(第 21 位显示在顶部,第 19 位显示在其下方,最后为空值)。

比较代码如下

    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;
        }
    }

我通过调用

ArrayList 进行排序
Collections.sort(myList, new CallDateComparator());
Collections.sort(myList, new AppointmentDateComparator());

上面需要更改什么才能使约会日期按升序排序,如果为空,则保持较早​​的排序不变?

我的方法基于评论的格式:

基本上我会做一个更大的比较器而不是两个不同的比较器。

 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;
        }
    }