JDK8的lambda方式List多字段排序List.stream().sorted()
多字段排序
java
List<HumanDetailDownloadVO> collect = dataList.stream().sorted(
// 日期 倒序
Comparator.comparing(HumanDetailDownloadVO::getDate_str, Comparator.reverseOrder())
// 仓id 倒序
.thenComparing(HumanDetailDownloadVO::getSite_code, Comparator.reverseOrder())
// 用户名升序 自定义比较器 拼音排序
.thenComparing(HumanDetailDownloadVO::getUser_name, new ChineseComparator())
// 大类code 升序
.thenComparing(HumanDetailDownloadVO::getPosition_code, Comparator.naturalOrder())
// 小类code 升序
.thenComparing(HumanDetailDownloadVO::getTask_code, Comparator.naturalOrder()))
.collect(Collectors.toList());
拼音排序:
引入jar包:
java
<!-- 汉字拼音排序-->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
自定义比较器:
package XXXXXXXXXXXXXXXX;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ChineseComparator implements Comparator<String> {
HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
@Override
public int compare(String o1, String o2) {
String pinyin1 = null;
String pinyin2 = null;
try {
pinyin1 = PinyinHelper.toHanYuPinyinString(o1, pinyinOutputFormat, " ", true);
pinyin2 = PinyinHelper.toHanYuPinyinString(o2, pinyinOutputFormat, " ", true);
System.out.println("pinyin1=" + pinyin1 + ", pinyin2=" + pinyin2);
} catch (Exception e) {
e.printStackTrace();
}
return pinyin1.compareTo(pinyin2);
}
}
结束。