fastjson java 字段排序_java stream sorted 多字段排序

package com.qimh.springbootfiledemo;

import com.alibaba.fastjson.JSON;

import com.google.common.collect.Lists;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Comparator;

import java.util.Date;

import java.util.List;

import java.util.stream.Collectors;

/**

* @author

*/

public class OrderList {

private String orderStatus;

private Date createDate;

private String orderNo;

public String getOrderStatus() {

return orderStatus;

}

public void setOrderStatus(String orderStatus) {

this.orderStatus = orderStatus;

}

public Date getCreateDate() {

return createDate;

}

public void setCreateDate(Date createDate) {

this.createDate = createDate;

}

public String getOrderNo() {

return orderNo;

}

public void setOrderNo(String orderNo) {

this.orderNo = orderNo;

}

public OrderList(String orderStatus, Date createDate, String orderNo) {

this.orderStatus = orderStatus;

this.createDate = createDate;

this.orderNo = orderNo;

}

public static void main(String[] args) throws ParseException {

List lists = Lists.newArrayList();

lists.add(new OrderList("2",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-16"),"0001"));

lists.add(new OrderList("gc002",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-09"),"0005"));

lists.add(new OrderList("2",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-15"),"0002"));

lists.add(new OrderList("gc001",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-11"),"0005"));

lists.add(new OrderList("0",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-13"),"0003"));

lists.add(new OrderList("0",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-12"),"0004"));

lists.add(new OrderList("gc001",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-10"),"0006"));

lists.add(new OrderList("gc002",new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-08"),"0006"));

//1.订单状态和订单创建时间

List DiffList = lists.stream().filter(orderList -> "0".equals(orderList.getOrderStatus()) || "2".equals(orderList.getOrderStatus()))

.sorted(Comparator.comparing(OrderList::getOrderStatus).reversed()

.thenComparing(OrderList::getCreateDate).reversed())

.collect(Collectors.toList());

System.out.println("DiffList:" + JSON.toJSONString(DiffList));

//2.订单创建时间

List myOrderList = lists.stream().filter(orderList -> !"0".equals(orderList.getOrderStatus()) && !"2".equals(orderList.getOrderStatus()))

.sorted(Comparator.comparing(OrderList::getCreateDate).reversed())

.collect(Collectors.toList());

System.out.println("myOrderList:" + JSON.toJSONString(myOrderList));

List sumOrderList = Lists.newArrayList();

sumOrderList.addAll(DiffList);

sumOrderList.addAll(myOrderList);

System.out.println("sumOrderList:" + JSON.toJSONString(sumOrderList));

// //2.分组

// Map> callRecordMap = lists.stream().collect(Collectors.groupingBy(OrderList::getOrderStatus));

//

// List tmpList = Lists.newArrayList();

// for (Map.Entry> entry:callRecordMap.entrySet()){

// String Orderstatus = entry.getKey();

// List orderlists = entry.getValue();

// System.out.println("Orderstatus:"+Orderstatus);

// System.out.println("orderlists:"+ JSON.toJSONString(orderlists));

// tmpList.addAll(orderlists);

//

// }

// System.out.println(JSON.toJSONString(tmpList));

}

}

运行结果:

DiffList:[{"createDate":1586707200000,"orderNo":"0003","orderStatus":"0"},{"createDate":1586620800000,"orderNo":"0004","orderStatus":"0"},{"createDate":1586966400000,"orderNo":"0001","orderStatus":"2"},{"createDate":1586880000000,"orderNo":"0002","orderStatus":"2"}]

myOrderList:[{"createDate":1586534400000,"orderNo":"0005","orderStatus":"gc001"},{"createDate":1586448000000,"orderNo":"0006","orderStatus":"gc001"},{"createDate":1586361600000,"orderNo":"0005","orderStatus":"gc002"},{"createDate":1586275200000,"orderNo":"0006","orderStatus":"gc002"}]

sumOrderList:[{"createDate":1586707200000,"orderNo":"0003","orderStatus":"0"},{"createDate":1586620800000,"orderNo":"0004","orderStatus":"0"},{"createDate":1586966400000,"orderNo":"0001","orderStatus":"2"},{"createDate":1586880000000,"orderNo":"0002","orderStatus":"2"},{"createDate":1586534400000,"orderNo":"0005","orderStatus":"gc001"},{"createDate":1586448000000,"orderNo":"0006","orderStatus":"gc001"},{"createDate":1586361600000,"orderNo":"0005","orderStatus":"gc002"},{"createDate":1586275200000,"orderNo":"0006","orderStatus":"gc002"}]

参考连接:

https://www.cnblogs.com/michaelShao/p/10893439.html(楼主说需要实现comparable接口,亲测 未实现comparable接口 不报错)

https://www.cnblogs.com/kuanglongblogs/p/11230250.html

https://www.cnblogs.com/liuheng0315/p/7112239.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值