java list多字段排序_java中list通过多条件排序
实现的效果类似于这样,首先通过一级类别id排序,其次是二级类别,最后是二级类别中的各项合计排序;
方法一
/**
* 单品容器排序
*
* @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;
}
}
}
java list多字段排序_java中list通过多条件排序相关推荐
- java如何对list进行排序_java中如何对list进行排序
java类中用Collections的sort方法对一个list进行排序,比较简单 先建一个User对象 public class User { public String name; public ...
- java list 排序_Java中List的排序
场景 Bean定义如下,仅有一个类型为Integer的age字段. @NoArgsConstructor @AllArgsConstructor(staticName = "of" ...
- JAVA中希尔排序去的讲解_java 中基本算法之希尔排序的实例详解
java 中基本算法之希尔排序的实例详解 希尔排序(Shell Sort)是插入排序的一种.也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法.该方法因DL.Shel ...
- java 排序_Java中常见的排序算法有哪些?---选择排序
排序相关的的基本概念 排序: 将一组杂乱无章的数据按一定的规律顺次排列起来. 数据表( data list): 它是待排序数据对象的有限集合. 排序码(key):通常数据对象有多个属性域, 即多个数据 ...
- java中queue排序_Java中常见的排序算法有哪些?---选择排序
排序相关的的基本概念 排序: 将一组杂乱无章的数据按一定的规律顺次排列起来. 数据表( data list): 它是待排序数据对象的有限集合. 排序码(key):通常数据对象有多个属性域, 即多个数据 ...
- java map字典序_java中对map的字典序排序
java中对map的字典序排序,算法验证比对微信官网https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115& ...
- java map 自动排序_Java中Map的排序
Map的种类 在Java中,Map的主要作用是存储键值对.由于是根据键得到值,所以不允许键重复.它主要有如下几个类别: HashMap: 最常用的Map,它根据键的HashCode值存储数据,根据键可 ...
- java util 排序_Java中常见的排序方法
本博主要介绍Java中几种常见的排序算法: /* 排序方法的演示 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排序(直接选择排序.堆排序) 4)归并排序 5)分 ...
- java中的排序方法_Java中8种常见的排序方法
本博主要介绍Java中几种常见的排序算法: /* 排序方法的演示 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排序(直接选择排序.堆排序) 4)归并排序 5)分 ...
最新文章
- 不要依赖代码中的异常
- pandas使用date_range函数按照指定的频率(freq)和指定的个数(periods)生成dataframe的时间格式数据列、基于dataframe的日期数据列生成日期索引
- iOS快速集成检查更新
- linux 无锁队列覆盖问题,无锁队列杂谈
- Entity Framework在WCF中序列化的问题
- CodeForces - 1516D Cut(思维+倍增)
- 在主界面的右下端设置系统时间
- 深度学习图像超分辨率最新综述:从模型到应用
- Linux Kernel Git国内镜像源
- 戴尔服务器哪1顶型号好,戴尔PowerEdge R730xd新一代服务器评测
- 从进化的角度看为啥要均贫富
- mysql中存储过程和函数区别
- 数学符号——指示函数(样子像空心的1的一个数学符号)
- 设计模式-手机生产-抽象工厂模式
- Detecting Faces in Images: A Survey( 翻译)
- 系统集成项目管理工程师(软考中级)—— 第十七章 项目沟通和干系人管理 笔记分享
- cgroup架构及控制文件介绍
- 7-1 sdut-循环-7-统计正数和负数的个数(II) (10 分)
- 计算机视觉基础知识(第一章)
- 三国刘备十大名言:三分天下要靠“混