您的位置:首页 > 大数据 > 人工智能

ComparatorChain对List对象的属性排序

2016-08-16 17:35 381 查看
一、按升序 / 降序对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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: