美文网首页
stream流对list排序

stream流对list排序

作者: 尘埃里的玄 | 来源:发表于2024-05-07 08:52 被阅读0次
    List<LinkedHashMap<String, Object>> roomList = new ArrayList<>();
    
    if (StringUtils.isNotBlank(sortField)) {
                List<HashMap<String, Object>> rooms = new ArrayList<>();
                if ("asc".equals(sortStyle)) {
    //中文排序
                    if ("ConsumerName".equals(sortField) || "AmMeter_Name".equals(sortField)) {
                        rooms = roomList.stream().sorted((a, b) ->
                                Collator.getInstance(Locale.CHINA).compare(a.get(sortField).toString(), b.get(sortField).toString())
                        ).collect(Collectors.toList());
                    } else {
    //数字排序
                        rooms = roomList.stream().sorted(((o1, o2) -> Double.valueOf(o1.get(sortField).toString()).compareTo(Double.valueOf(o2.get(sortField).toString())))).collect(Collectors.toList());
                    }
                } else {
                    if ("ConsumerName".equals(sortField) || "AmMeter_Name".equals(sortField)) {
                        rooms = roomList.stream().sorted((a, b) ->
                                Collator.getInstance(Locale.CHINA).compare(b.get(sortField).toString(), a.get(sortField).toString())
                        ).collect(Collectors.toList());
                    } else {
                        rooms = roomList.stream().sorted(((o1, o2) -> Double.valueOf(o2.get(sortField).toString()).compareTo(Double.valueOf(o1.get(sortField).toString())))).collect(Collectors.toList());
                    }
                }
                map.put("RoomList", rooms);
            } else {
                map.put("RoomList", roomList);
            }
            map.put("sumAfterMulCoefficientValue", sumAfterMulCoefficientValue);
    

    相关文章

      网友评论

          本文标题:stream流对list排序

          本文链接:https://www.haomeiwen.com/subject/qkprfjtx.html