详细:JAVA 对集合list的实体按某一字段(如日期)排序(降序,Collections的sort方法)

最近有时间,就整理整理自己做过的项目~这是某医院的微信网页开发项目。
话不多说,进入正题

情景:现在有一个List,需要对病人每一次的体检记录(Quota)按照日期进行降序排序(即近期放在最前面) 而Collections的sort方法默认是升序排列,如果需要降序排列时就需要重写compare方法。

首先放出compare的注释

 * @param o1 the first object to be compared.
     * @param o2 the second object to be compared.
     * @return a negative integer, zero, or a positive integer as the
     *         first argument is less than, equal to, or greater than the
     *         second.
     * @throws NullPointerException if an argument is null and this
     *         comparator does not permit null arguments
     * @throws ClassCastException if the arguments' types prevent them from
     *         being compared by this comparator.
     */
    int compare(T o1, T o2);

也就是说默认o1和o2比较,如果o1-o2返回负数,则把o1放在o2前面。(升序)

Collections.sort(list, new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;//注意这一行代码
            }
        });

可我现在要实现降序,那么思路应该是

Collections.sort(list, new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;//注意这一行代码
            }
        });

解释完compare方法,下面是该情景解决方案:

    //按日期排序(降序)
    private void ListSort(List<Quota> list) {
        //Collections的sort方法默认是升序排列,如果需要降序排列时就需要重写compare方法
        Collections.sort(list, new Comparator<Quota>() {
            @Override
            public int compare(Quota o1, Quota o2) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    //获取体检日期,并把其类型由String转成Date,便于比较。
                    Date dt1 = format.parse(o1.getDate());
                    Date dt2 = format.parse(o2.getDate());

                    //以下代码决定按日期降序排序,若将return“-1”与“1”互换,即可实现升序。
                    //getTime 方法返回一个整数值,这个整数代表了从 1970 年 1 月 1 日开始计算到 Date 对象中的时间之间的毫秒数。
                    if (dt1.getTime() > dt2.getTime()) {
                        return -1;
                    } else if (dt1.getTime() < dt2.getTime()) {
                        return 1;
                    } else {
                        return 0;
                    }

                } catch (Exception e) {
                    logger.info("日期排序出错:"+e);
                }
                return 0;
            }
        });
    }

此外,还有一种实现方式:

list.sort(new Comparator<Student>() {
    @Override
    public int compare(Quota o1, Quota o2) {
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    //获取体检日期,并把其类型由String转成Date,便于比较。
                    Date dt1 = format.parse(o1.getDate());
                    Date dt2 = format.parse(o2.getDate());

                    //以下代码决定按日期降序排序,若将return“-1”与“1”互换,即可实现升序。
                    //getTime 方法返回一个整数值,这个整数代表了从 1970 年 1 月 1 日开始计算到 Date 对象中的时间之间的毫秒数。
                    if (dt1.getTime() > dt2.getTime()) {
                        return -1;
                    } else if (dt1.getTime() < dt2.getTime()) {
                        return 1;
                    } else {
                        return 0;
                    }
    }
});

以上便是全部过程,有啥问题,欢迎评论~~~(๑•ᴗ•๑)

觉得还不错可以点个赞哦~ 谢谢(๑•ᴗ•๑)

发布了52 篇原创文章 · 获赞 84 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_39380155/article/details/99457300