java报错:cannot resolve method compareTo(java.lang.double)

 

排序不正确写法:

        List<Map<String,Object>> sortList2 = list.stream().sorted((o1, o2) -> {
            if (o1.get("DAY_ACTUAL").toString().compareTo(o2.get("DAY_ACTUAL").toString()) > 0) {
                return -1;
            } else  return 1;
        }).collect(Collectors.toList());

 

排序不正确,需将 字符串转为 double 再排序

 

报错写法:

        List<Map<String,Object>> sortList2 = list.stream().sorted((o1, o2) -> {
            if ((Double)(o1.get("DAY_ACTUAL").toString().compareTo(Double)(o2.get("DAY_ACTUAL").toString()) > 0) {
                return -1;
            } else  return 1;
        }).collect(Collectors.toList());

 

解决方法:

将基础类型double转为java封装的Double类

调整后写法:

        List<Map<String,Object>> sortList2 = list.stream().sorted((o1, o2) -> {
            if (new Double(o1.get("DAY_ACTUAL").toString()).compareTo(new Double(o2.get("DAY_ACTUAL").toString())) > 0) {
                return -1;
            } else  return 1;
        }).collect(Collectors.toList());

 

posted @ 2022-10-21 10:51  moonsoft  阅读(50)  评论(0编辑  收藏  举报