Java排序 Collections.sort排序

 Collections.sort  多条件排序

return  a.compareTo(b);    注意 a  b 参数的类型不可为 String !!!  可用Integer   Double 

/**
     * 收益率倒序  || 权重正序&&收益率倒序
     * @param fundSubModuleLists
     * @param sortStatus
     */
    private void getSortFundData(List<FundSubModuleList> fundSubModuleLists, final Integer sortStatus) {
        // 收益率排序
        if (sortStatus == 1 ) {
            Collections.sort(fundSubModuleLists, new Comparator<FundSubModuleList>() {
                @Override
                public int compare(FundSubModuleList o1, FundSubModuleList o2) {
                    return chgValueSort(o1.getChgValue(), o2.getChgValue(), sortStatus);
                }
            });
        }else{
            // 权重正序&&收益率倒序
            Collections.sort(fundSubModuleLists,new Comparator<FundSubModuleList>(){
                @Override
                public int compare(FundSubModuleList o1, FundSubModuleList o2) {
                    Integer theWeight = Integer.valueOf(o1.getTheWeight() == null ? "1000" : o1.getTheWeight());
                    Integer theWeight2 = Integer.valueOf(o2.getTheWeight() == null ? "1000" : o2.getTheWeight());
                    if(!theWeight.equals(theWeight2)){
                        return theWeight.compareTo(theWeight2);
                    }
                    return chgValueSort(o1.getChgValue(), o2.getChgValue(), sortStatus);
                }
            });
        }
        for (int i = 0, j = 0; i < fundSubModuleLists.size(); i++,j++) {
            FundSubModuleList fundSubModuleList = fundSubModuleLists.get(i);
            fundSubModuleList.setSortId(i + 1);
        }
    }

    /**
     * 基于收益率倒序
     * @param chgValue
     * @param chgValue2
     * @param sortStatus
     * @return
     */
    private int chgValueSort(String chgValue, String chgValue2, Integer sortStatus) {
        Double d;
        Double d2;
        if ("--".equals(chgValue) || "null".equals(chgValue) || chgValue == null) {
            d = Double.MIN_VALUE;
        } else {
            String s = chgValue.split("%")[0].replaceAll(",","");
            d = Double.parseDouble(s);
        }
        if ("--".equals(chgValue2) || "null".equals(chgValue2) || chgValue2 == null) {
            d2 = Double.MIN_VALUE;
        } else {
            String s2 = chgValue2.split("%")[0].replaceAll(",","");
            d2 = Double.parseDouble(s2);
        }
        return d2.compareTo(d);
    }

猜你喜欢

转载自blog.csdn.net/xiangwang2016/article/details/109453944