对List中的对象按某个字段进行排序

这个是参考了其他博主写的文章

原文链接:https://blog.csdn.net/juligang320/article/details/82885738#commentBox

这在自己的博客,进行了稍微的更新,以便记录自己的学习过程

实体对象

public class Logs implements Serializable, Comparable<Logs> {

    private String id;
    private String username;
    private String contents;
    private String consequence;
    private String ip;
    private Long times;

    @Override
    public int compareTo(Logs o) {
        return 0;
    }

}

解析: Logs实体,要继承Comparable<T>接口, 然后再实现一个public int compareTo(Logs o) 方法.

现在,我要对List<Logs> 中的数据按 times 字段进行排序

    @Test
    public void testMath(){
        List<Logs> list = new ArrayList<Logs>();
        list.add(new Logs("1","chen", "djifand", "jdifa", "jdfid", 49L));
        list.add(new Logs("134","ce", "cae", "jdifa", "jdfid", 123L));
        list.add(new Logs("3","cet", "djifand", "jdifa", "jdfid", 99L));
        list.add(new Logs("4","egge", "djifand", "jdifa", "jdfid", 111L));
        list.add(new Logs("15","chen", "djifand", "jdifa", "jdfid", 59L));
        list.add(new Logs("6","chen2", "cae", "jdifa", "jdfid", 123L));
        list.add(new Logs("87","cet", "djifand", "jdifa", "jdfid", 213L));
        list.add(new Logs("8","egge", "djifand", "jdifa", "jdfid", 201L));
        list.add(new Logs("9","egge2", "djifand", "jdifa", "jdfid", 269L));
        list.add(new Logs("10","ce", "cae", "jdifa", "jdfid", 121L));
        list.add(new Logs("11","chen3", "djifand", "jdifa", "jdfid", 1123L));
        list.add(new Logs("5","cet2", "djifand", "jdifa", "jdfid", 12L));

        System.out.println("---- 排序前的输出  ----- ");
        for (Logs logs : list) {
            System.out.println("数据: "+logs);
        }
        System.out.println("---- 排序后的结果  ----- ");
        List<Logs> search = search(list);

        for (Logs logs : search) {
            System.out.println("-->"+logs);
        }

    }


     // 方式1: 降序排列
     public List<Logs> search(List<Logs> logsList){
        Collections.sort(logsList, new Comparator<Logs>() {
            @Override
            public int compare(Logs o1, Logs o2) {
                if ((o1.getTimes() > o2.getTimes())){
                    return -1;
                }
                if (o1.getTimes() == o2.getTimes()){
                    return 0;
                }
                return 1;
            }
        });
        return logsList;
    }

     // 方式2: 升序排列
     public List<Logs> search(List<Logs> logsList){
        Collections.sort(logsList, new Comparator<Logs>() {
            @Override
            public int compare(Logs o1, Logs o2) {
                if ((o1.getTimes() > o2.getTimes())){
                    return 1;
                }
                if (o1.getTimes() == o2.getTimes()){
                    return 0;
                }
                return -1;
            }
        });
        return logsList;
    }
    // 方式3: 降序排列 
    public List<Logs> search(List<Logs> logsList){
        Collections.sort(logsList, new Comparator<Logs>() {
            @Override
            public int compare(Logs o1, Logs o2) {
                return -o1.getTimes().compareTo(o2.getTimes());
            }
        });
        return logsList;
    }

    // 方式4: 升序排列
    public List<Logs> search(List<Logs> logsList){
        Collections.sort(logsList, new Comparator<Logs>() {
            @Override
            public int compare(Logs o1, Logs o2) {
                return o1.getTimes().compareTo(o2.getTimes());
            }
        });
        return logsList;
    }

至此,List<Logs> 里面的Long数据类型比较完毕.

©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页