java8使⽤stream进⾏排序(多字段排序、升序、降序)⽂章包含“标题”、“发布时间”、“是否推荐”、“是否置顶”四个属性。
import AllArgsConstructor;
import Data;
import NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Article {
/**
* ⽂章标题
*/
private String title;
/**
* 发布时间
*/
private String releaseTime;
/**
* 是否推荐:Y 推荐;N 不推荐
*/
private String recommend;
/**
* 是否置顶: Y 置顶;N 不置顶
*/
private String top;
}
测试代码
import ArrayList;
import Comparator;
import List;
import Collectors;
public class Demo {
public static void main(String[] args){
List<Article> list =init();
sort(list);
sort2(list);
}
/**
* 初始化数据
* @return
*/
private static List<Article>init(){
List<Article> list =new ArrayList<>();
list.add(new Article("⽂章⼀","2021-11-01","Y","Y"));
list.add(new Article("⽂章⼆","2021-11-02","Y","Y"));
list.add(new Article("⽂章三","2021-11-01","Y","N"));
list.add(new Article("⽂章⼀","2021-11-02","Y","N"));
list.add(new Article("⽂章⼀","2021-11-01","N","Y"));
list.add(new Article("⽂章⼀","2021-11-02","N","Y"));
list.add(new Article("⽂章⼀","2021-11-01","N","N"));
list.add(new Article("⽂章⼀","2021-11-02","N","N"));
return list;
}
....
}
排序⽅法
/
**
* 按照推荐、置顶、发布时间来排序
* @param list
*/
private static void sort(List<Article> list){
List<Article> sortList = list.stream()
// 【X】
.sorted(Comparatorparing(Article::verseOrder())
// 【Y】
.thenComparing(Article::verseOrder())
// 【Z】
.
thenComparing(Article::verseOrder()))
.List());
sortList.stream().forEach(System.out::println);
}
// 运⾏结果:
// Article(title=⽂章⼆, releaseTime=2021-11-02, recommend=Y, top=Y)
// Article(title=⽂章⼀, releaseTime=2021-11-01, recommend=Y, top=Y)
// Article(title=⽂章⼀, releaseTime=2021-11-02, recommend=Y, top=N)
// Article(title=⽂章三, releaseTime=2021-11-01, recommend=Y, top=N)
java stream// Article(title=⽂章⼀, releaseTime=2021-11-02, recommend=N, top=Y)
// Article(title=⽂章⼀, releaseTime=2021-11-01, recommend=N, top=Y)
/
/ Article(title=⽂章⼀, releaseTime=2021-11-02, recommend=N, top=N)
// Article(title=⽂章⼀, releaseTime=2021-11-01, recommend=N, top=N)
【X】:优先使⽤recommend字段排序,并且使⽤降序排列(verseOrder()),在结果中的体现是前四篇⽂章的recommend为‘Y’,后四篇的recommend为‘N’
【Y】:之后再使⽤top字段排序,并且使⽤降序排列(verseOrder()), 在结果中的体现是recommend相同的⽂章中,前两篇⽂章的top为‘Y’, 后两篇⽂章的top为‘N’
【Z】:最后使⽤发布时间字段进⾏排序,并且使⽤降序排列(verseOrder())
两种倒序⽅式
Comparatorparing(类::属性,verseOrder())
/**
* 按照推荐、置顶、发布时间来排序
* @param list
*/
private static void sort(List<Article> list){
List<Article> sortList = list.stream()
// 【X】
.sorted(Comparatorparing(Article::verseOrder())
// 【Y】
.thenComparing(Article::verseOrder())
// 【Z】
.thenComparing(Article::verseOrder()))
.List());
sortList.stream().forEach(System.out::println);
}
// 运⾏结果:
// Article(title=⽂章⼆, releaseTime=2021-11-02, recommend=Y, top=Y)
// Article(title=⽂章⼀, releaseTime=2021-11-01, recommend=Y, top=Y)
// Article(title=⽂章⼀, releaseTime=2021-11-02, recommend=Y, top=N)
// Article(title=⽂章三, releaseTime=2021-11-01, recommend=Y, top=N)
// Article(title=⽂章⼀, releaseTime=2021-11-02, recommend=N, top=Y)
// Article(title=⽂章⼀, releaseTime=2021-11-01, recommend=N, top=Y)
// Article(title=⽂章⼀, releaseTime=2021-11-02, recommend=N, top=N)
// Article(title=⽂章⼀, releaseTime=2021-11-01, recommend=N, top=N)
以传⼊的字段进⾏倒序排列
【X】:这⾥就类似于mysql中的 order by recommend desc
【Y】:结合【X】就是order by recommend desc, top desc
Comparatorparing(Article::verseOrder()).reversed()
private static void sort2(List<Article> list){
List<Article> sortList = list.stream()
.sorted(Comparatorparing(Article::verseOrder())
.thenComparing(Article::verseOrder())
.thenComparing(Article::verseOrder()).reversed())// 【X】
.List());
sortList.stream().forEach(System.out::println);
}
/
/ 运⾏结果:
// Article(title=⽂章⼀, releaseTime=2021-11-01, recommend=N, top=N)
// Article(title=⽂章⼀, releaseTime=2021-11-02, recommend=N, top=N)
// Article(title=⽂章⼀, releaseTime=2021-11-01, recommend=N, top=Y)
// Article(title=⽂章⼀, releaseTime=2021-11-02, recommend=N, top=Y)
// Article(title=⽂章三, releaseTime=2021-11-01, recommend=Y, top=N)
// Article(title=⽂章⼀, releaseTime=2021-11-02, recommend=Y, top=N)
// Article(title=⽂章⼀, releaseTime=2021-11-01, recommend=Y, top=Y)
// Article(title=⽂章⼆, releaseTime=2021-11-02, recommend=Y, top=Y)
【X】:这⾥的reversed()是将之前的排序进⾏整个倒序,可以发现这个⽅法的运⾏结果和上⼀个运⾏⽅法的运⾏结果是完全相反的
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论