在java中对ArrayList w.r.t Date进行排序

时间:2015-08-25 13:33:26

标签: java

我正在尝试对ArrayList w.r.t termDate进行排序 ArrayList包含firstName,lastName,email,startDate,termDate

日期可以为空或为空。 我必须相应地进行比较,并将所有空/空日期值放在最后。

Collections.sort(usersList, new Comparator<User>() {
                public int compare(User o1, User o2) {
                    if(o1.getTermDate() == null && o2.getTermDate() == null)
                        return 0; //They are both null, both equal
                    if(o1.getTermDate() == null && o2.getTermDate() != null)
                        return -1; // The first is null and the second is not, return the first as lower than the second
                    if(o1.getTermDate() != null && o2.getTermDate() == null)
                        return 1; //The first is not null and the second is, return the first as higher than the second
                    else
                        return o1.getTermDate().compare(o2.getTermDate()); //Return the actual comparison
                }
            });

它没有编译比较方法。

请指导。

1 个答案:

答案 0 :(得分:1)

您应该将return o1.getTermDate().compare(o2.getTermDate());更改为return o1.getTermDate().compareTo(o2.getTermDate());因为课程java.util.Date;没有名为compare的方法。