java list多字段排序_java中list通过多条件排序

实现的效果类似于这样,首先通过一级类别id排序,其次是二级类别,最后是二级类别中的各项合计排序;

sort.png

方法一

/**

* 单品容器排序

*

* @param productSellStatList

* @return

*/

private void getSortList(List productSellStatList) {

Collections.sort(productSellStatList, new Comparator() {

@Override

public int compare(ProductSellStat o1, ProductSellStat o2) {

if (o1.getParentProductTypeID() < o2.getParentProductTypeID()) {// 一级类目

return -1;

} else if (o1.getParentProductTypeID() == o2.getParentProductTypeID()) { // 一级类目

if (o1.getProductTypeID() < o2.getProductTypeID()) {

return 1;

} else if (o1.getProductTypeID() == o2.getProductTypeID()) { // 二级类目

if (o1.getRowTotal() > o2.getRowTotal()) { // 通过合计

return -1;

} else if (o1.getRowTotal() == o2.getRowTotal()) {// 通过合计

return 0;

} else {

return 1;

}

} else {

return 1;

}

} else {

return 1;

}

}

});

}

方法二

//productSellStatList为要排序的集合

List sortProductSellStatList = new ArrayList(productSellStatList.size()) ;

Map> tempMap = new HashMap>() ;

Comparator comparator = new TotalSaleComparator() ;

for (ProductSellStat p : productSellStatList) {

if (tempMap.isEmpty()) {

List tempList = new ArrayList() ;

tempList.add(p);

tempMap.put(p.getProductTypeID(), tempList) ;

} else {

if (tempMap.containsKey(p.getProductTypeID())) {

tempMap.get(p.getProductTypeID()).add(p);

} else {

// 为lis排序

for (List pList : tempMap.values()) {

Collections.sort(pList, comparator); // 排序

sortProductSellStatList.addAll(pList) ;

}

tempMap.clear();

}

}

if (tempMap.isEmpty()) {

List tempList = new ArrayList() ;

tempList.add(p);

tempMap.put(p.getProductTypeID(), tempList) ;

}

}

// 处理最后的内容

if (!tempMap.isEmpty()) {

// 为lis排序

for (List pList : tempMap.values()) {

Collections.sort(pList, comparator); // 排序

sortProductSellStatList.addAll(pList) ;

}

tempMap.clear();

}

productSellStatList.clear(); // 清空集合,方便垃圾回收

/**

* 销量汇总排序

*/

public class TotalSaleComparator implements Comparator {

@Override

public int compare(ProductSellStat o1, ProductSellStat o2) {

if (o1.getRowTotal() > o2.getRowTotal()) {

return -1;

} else if (o1.getRowTotal() == o2.getRowTotal()) {

return 0;

} else {

return 1;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值