java 集合按照 数值排序,如果数值一致 按照名字首字母排序

需求背景:

按照相似度排序,如果相似度一样,则按照名称进行排序

public static void main(String[] args) {
        //java保留两位小数计算 
        //分子
        String a = "1";
        //分母
        String b = "4";
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        //计算  cal值为0.25
        String cal = numberFormat.format(Float.parseFloat(a) / Float.parseFloat(b) * 100);
        
        List<Map<String, Object>> sps = new ArrayList<>();
        Map<String, Object> m1 = new HashMap<>();
        m1.put("similarity","32.22");
        m1.put("NAME","儿童随访测试");
        Map<String, Object> m11 = new HashMap<>();
        m11.put("similarity","100");
        m11.put("NAME","高血压测试服务包");
        Map<String, Object> m2 = new HashMap<>();
        m2.put("similarity","21.25");
        m2.put("NAME","残疾人康复服务");
        Map<String, Object> m3 = new HashMap<>();
        m3.put("similarity","66.3");
        m3.put("NAME","康复服务");
        Map<String, Object> m4 = new HashMap<>();
        m4.put("similarity","0");
        m4.put("NAME","口腔保健服务");
        Map<String, Object> m5 = new HashMap<>();
        m5.put("similarity","50");
        m5.put("NAME","特需上门服务");

        Map<String, Object> m6 = new HashMap<>();
        m6.put("similarity","84.1");
        m6.put("NAME","中医药");

        sps.add(m11);
        sps.add(m2);
        sps.add(m1);
        sps.add(m5);
        sps.add(m4);
        sps.add(m3);

        sps.add(m6);
        List<Map<String, Object>> spsi = sps;
        //排序
        Collator collator = Collator.getInstance(Locale.CHINA);
        Collections.sort(sps, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                String a1 = (String) o1.get("similarity");
                String a2 = (String) o2.get("similarity");
                if (CommonUtil.isStringEmptyOrNull(a1)){
                    a1 = "0";
                }else{
                    if (!a1.contains(".")){
                        a1 = a1 + ".00";
                    }
                    a1 = a1.replace(".","");
                }if (CommonUtil.isStringEmptyOrNull(a2)){
                    a2 = "0";
                }else{
                    if (!a2.contains(".")){
                        a2 = a2 + ".00";
                    }
                    a2 = a2.replace(".","");
                }
                String sp1 = (String) o1.get("bagName");
                String sp2 = (String) o2.get("bagName");
                if(CommonUtil.isStringEmptyOrNull(sp1)){
                    sp1 = "";
                }if(CommonUtil.isStringEmptyOrNull(sp2)){
                    sp2 = "";
                }

                BigDecimal f1 = new BigDecimal(a1);
                BigDecimal f2 = new BigDecimal(a2);
                if (!a1.equals(a2)){
                    return f2.subtract(f1).intValue();
                }else{
                    return   collator.compare(sp1, sp2);
                }
            }
        });
        System.out.println(JSONArray.fromObject(sps).toString());
    }

 

posted @ 2021-04-14 16:24  ジ绯色月下ぎ  阅读(850)  评论(0编辑  收藏  举报