JDK8的lambda方式List多字段排序List.stream().sorted()
多字段排序
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包:
<!-- 汉字拼音排序-->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
自定义比较器:文章来源:https://www.toymoban.com/news/detail-694996.html
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);
}
}
结束。文章来源地址https://www.toymoban.com/news/detail-694996.html
到了这里,关于JDK8的lambda方式List多字段排序List.stream().sorted()的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!