List排序:list实体和listmap排序

listmap排序:

//ListMap排序
        Collections.sort(allNum, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Double total1 = Double.valueOf((String) o1.get("total"));
                Double total2 = Double.valueOf((String) o2.get("total"));
                return total2.compareTo(total1);
            }
        });

list实体排序:

reportData.sort((x, y) -> CharSequence.compare(x.getOrderBy(), y.getOrderBy()));

  

 

 //想要排序的JSON数组
        //快速排序,重写compare方法,完成按指定字段比较,完成排序
        //https://blog.csdn.net/weixin_41922289/article/details/98496792?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1
      /*  List<JSONObject> jsonlist = JSONObject.parseArray(resultJson.toJSONString(), JSONObject.class);
        Collections.sort(jsonlist, new Comparator<JSONObject>() {
            //重写compare方法
            @Override
            public int compare(JSONObject a, JSONObject b) {
                String valA = "";
                String valB = "";
                try {
                    valA = a.getString("sort_by");
                    valB = b.getString("sort_by");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //是升序还是降序
                return -valB.compareTo(valA);
               *//* if (is_desc){
                    return -valA.compareTo(valB);
                } else {
                    return -valB.compareTo(valA);
                }*//*
            }
        });
        //将排序后结果放入结果jsonArray
      *//*  for (int i = 0; i < jsonlist.size(); i++) {
            resultJson.add(jsonlist.get(i));
        }*//*
        resultJson = JSONArray.parseArray(JSON.toJSONString(jsonlist));*/
        /*List<Map<String, Object>> projList = (List) resultJson;
        for (Map<String, Object> map : users) {
            String username1 = (String) map.get("username");
            //将多条项目数据以children形式放到用户信息里
            List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
            for (Map<String, Object> objectMap : projList) {
                if (username1.equals(objectMap.get("userName"))) {
                    children.add(objectMap);
                }
            }
            //放第一条数据到用户信息里
            if (children != null && children.size() > 0) {
                Map<String, Object> firstMap = children.get(0);
                for (String s : firstMap.keySet()) {
                    map.put(s, firstMap.get(s));
                }
                children.remove(0);
            }
            map.put("children", children);
        }*/

  

posted on 2020-05-18 16:56  江上酒,故人倾  阅读(461)  评论(0编辑  收藏  举报