• 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());
        }
  • 相关阅读:
    jsp大文件(视频)上传问题
    python多版本的pip共存问题解决办法
    buntu系统安装rpm包的方法
    如何查看linux是32位还是64位
    python的if else
    python获取列表唯一值
    python列表转数组
    python文件操作write与writelines的区别
    python中字符串str的strip()方法
    ubuntu16.04安装cloudcompare及打开方法
  • 原文地址:https://www.cnblogs.com/xuchao0506/p/14658561.html
Copyright © 2020-2023  润新知