ComparatorChain对List对象的属性排序

    xiaoxiao2026-08-24  2

    一、按升序 / 降序对List中的对象进行排序

    需要使用到对象

    org.apache.commons.beanutils.BeanComparator; org.apache.commons.collections.comparators.ComparatorChain; 以下是简单实现 public void test(){ List<FTCoin> list = new ArrayList<FTCoin>(); FTCoin ft1 = new FTCoin(8,"ad"); FTCoin ft2 = new FTCoin(4,"he"); FTCoin ft3 = new FTCoin(9,"bo"); FTCoin ft4 = new FTCoin(5,"zh"); list.add(ft1); list.add(ft2); list.add(ft3); list.add(ft4); ComparatorChain chain = new ComparatorChain(); //false升序排序,true降序排序 chain.addComparator(new BeanComparator("id"),false); chain.addComparator(new BeanComparator("coin"),true); Collections.sort(list,chain); for(FTCoin ft:list){ System.out.println(ft.getId()+","+ft.getCoin()); } } 二、输出结果 4,he 5,zh 8,ad 9,bo

    转载请注明原文地址: https://ju.6miu.com/read-1311516.html
    最新回复(0)