首页 > 代码库 > List对象分组排序算法
List对象分组排序算法
场景:
List里面的对象是订单的节点,比如我们快递的物流状态,这个是需要有序的,所以需要根据订单号进行分组排序。
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.List;import java.util.Map;public class GroupSortTest { public static void main(String[] args) { List<Order> list = new ArrayList<Order>(); Order o1 = new Order("10086","3",(long) 1008603); Order o2 = new Order("10086","2",(long) 1008602); Order o3 = new Order("10086","1",(long) 1008601); Order o4 = new Order("10010","1",(long) 1001001); Order o5 = new Order("10000","3",(long) 1000003); Order o6 = new Order("10010","3",(long) 1001003); Order o7 = new Order("10000","1",(long) 1000001); Order o8 = new Order("10000","2",(long) 1000002); Order o9 = new Order("10010","2",(long) 1001002); list.add(o1); list.add(o2); list.add(o3); list.add(o4); list.add(o5); list.add(o6); list.add(o7); list.add(o8); list.add(o9); Map<String, List<Order>> map = OrderSortByGroup(list); for(Map.Entry<String, List<Order>> entry : map.entrySet()){ for(Order o : entry.getValue() ){ System.out.println(entry.getKey() +"--"+o.getSeq()+"--"+o.getStatusId()); } } } public static Map<String, List<Order>> OrderSortByGroup(List<Order> list){ Map<String, List<Order>> map = new HashMap<String, List<Order>>(); for(Order order : list) { List<Order> staList = map.get(order.orderNo); if(staList==null){ staList = new ArrayList<Order>(); } Order od = new Order(); od.setSeq(order.seq);od.setStatusId(order.statusId); staList.add(od); Collections.sort(staList, new Comparator<Order>() { @Override public int compare(Order o1, Order o2) { return o1.getSeq().compareTo(o2.getSeq()); } }); map.put(order.orderNo, staList); } return map; } }class Order { String orderNo; //订单号 String seq; //订单节点序号 Long statusId; //订单节点主键 public Order() {} public Order(String orderNo, String seq, Long statusId) { super(); this.orderNo = orderNo; this.seq = seq; this.statusId = statusId; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public String getSeq() { return seq; } public void setSeq(String seq) { this.seq = seq; } public Long getStatusId() { return statusId; } public void setStatusId(Long statusId) { this.statusId = statusId; }}
List对象分组排序算法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。