zoukankan      html  css  js  c++  java
  • Java中的集合排序

    1. 定义排序

    class ComparatorDefault implements Comparator {
            public int compare(Object arg0, Object arg1) {
                OrderDetailReturn.BidsEntity bid0 = (OrderDetailReturn.BidsEntity) arg0;
                OrderDetailReturn.BidsEntity bid1 = (OrderDetailReturn.BidsEntity) arg1;
                //按ID排序
                return bid0.getId() - bid1.getId();
            }
        }
    
        class ComparatorCredit implements Comparator {
            public int compare(Object arg0, Object arg1) {
                OrderDetailReturn.BidsEntity bid0 = (OrderDetailReturn.BidsEntity) arg0;
                OrderDetailReturn.BidsEntity bid1 = (OrderDetailReturn.BidsEntity) arg1;
                //按ID排序
                if (bid0.getBearer() == null || bid1.getBearer() == null) return 0;
                return bid0.getBearer().getScore() - bid1.getBearer().getScore();
            }
        }
    
        class ComparatorPrice implements Comparator {
            public int compare(Object arg0, Object arg1) {
                OrderDetailReturn.BidsEntity bid0 = (OrderDetailReturn.BidsEntity) arg0;
                OrderDetailReturn.BidsEntity bid1 = (OrderDetailReturn.BidsEntity) arg1;
                //按ID排序
                return bid0.getPrice() - bid1.getPrice();
            }
        }

    2. 调用排序

    if(bidList == null) return;
            Comparator comparator;
            if (style == 1) {
                comparator = new ComparatorCredit();
            } else if (style == 2) {
                comparator = new ComparatorPrice();
            } else {
                comparator = new ComparatorDefault();
            }
            Collections.sort(bidList, comparator);
  • 相关阅读:
    逆元
    C++快读
    最长单调上升子序列(LIS) O(nlogn)求法
    【简●解】巴厘岛的雕塑
    【简●解】学校食堂
    【简●解】[HNOI2005]星际贸易
    差分约束系统小结
    【简•解】花园
    最小生成树小结
    概率及期望DP小结
  • 原文地址:https://www.cnblogs.com/atwind/p/4633040.html
Copyright © 2011-2022 走看看