一个List对象按照另一个List对象的数据顺序来排序

本文介绍了一个Java程序案例,该程序通过自定义的排序方法对一系列包含特定ID和年龄的对象进行排序。首先按照对象的ID进行排序,然后按照年龄进行二次排序。通过对排序逻辑的实现和展示,读者可以了解到如何在Java中利用Collections.sort方法结合自定义比较器实现复杂排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class ListOrder {
    public static void main(String[] args) {

        List<String> orderRegulation = Arrays.asList("G102", "G103", "G108", "29", "28", "25", "24", "G109", "G105", "26", "21");
        List<Posts> targetList = new ArrayList<Posts>();

        Posts post3 = new Posts();
        post3.setId("G102");
        post3.setName("xiaoli3");
        post3.setAge("23");

        Posts post2 = new Posts();
        post2.setId("G103");
        post2.setName("xiaoli2");
        post2.setAge("22");

        Posts post4 = new Posts();
        post4.setId("G109");
        post4.setName("xiaoli4");
        post4.setAge("25");

        Posts post7 = new Posts();
        post7.setId("G107");
        post7.setName("xiaoli7");
        post7.setAge("21");

        Posts post8 = new Posts();
        post8.setId("G101");
        post8.setName("xiaoli8");
        post8.setAge("26");

        Posts post5 = new Posts();
        post5.setId("G105");
        post5.setName("xiaoli5");
        post5.setAge("27");

        Posts post1 = new Posts();
        post1.setId("G104");
        post1.setName("xiaoli1");
        post1.setAge("29");

        Posts post6 = new Posts();
        post6.setId("G106");
        post6.setName("xiaoli6");
        post6.setAge("23");

        Posts post9 = new Posts();
        post9.setId("G106");
        post9.setName("xiaoli9");
        post9.setAge("25");

        Posts post10 = new Posts();
        post10.setId("G104");
        post10.setName("xiaoli10");
        post10.setAge("28");

        targetList.add(post1);
        targetList.add(post2);
        targetList.add(post3);
        targetList.add(post4);
        targetList.add(post5);
        targetList.add(post6);
        targetList.add(post7);
        targetList.add(post8);
        targetList.add(post9);
        targetList.add(post10);
        System.out.println("排列前的数据:");
        targetList.forEach(t -> System.out.print(t.getId() + t.getName() + "~" + t.getAge() + "  "));
        System.out.println();

        setListOrder(orderRegulation, targetList);

        System.out.println("排序的规则:");
        orderRegulation.forEach(t -> System.out.print(t + " "));
        System.out.println();
        System.out.println("排列后的数据:");
        targetList.forEach(t -> System.out.print(t.getId() + t.getName() + "~" + t.getAge() + " "));

    }

    //平时排序可使用其中一种,下面是综合两个条件排序
    public static void setListOrder(List<String> orderRegulation, List<Posts> targetList) {
        //按照Posts的Id来排序
        Collections.sort(targetList, ((o1, o2) -> {
            int io1 = orderRegulation.indexOf(o1.getId());
            int io2 = orderRegulation.indexOf(o2.getId());

            if (io1 != -1) {
                io1 = targetList.size() - io1;
            }
            if (io2 != -1) {
                io2 = targetList.size() - io2;
            }

            return io2 - io1;
        }));

        //按照Posts的age来排序
        Collections.sort(targetList, ((o1, o2) -> {
            int io1 = orderRegulation.indexOf(o1.getAge());
            int io2 = orderRegulation.indexOf(o2.getAge());

            if (io1 != -1) {
                io1 = targetList.size() - io1;
            }
            if (io2 != -1) {
                io2 = targetList.size() - io2;
            }

            return io2 - io1;
        }));
    }
}


public class Posts {
    private String id;
    private String name;
    private String age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值