JSONArray 按某字段进行排序

JSONArray jsonArray = JSON.parseArray(searchCondition.get("data").toString());
//对jsonArray中的数据按autoId进行正排
Collections.sort(jsonArray, new Comparator() {
   @Override
   public int compare(Object o1, Object o2) {
                 JSONObject js1 = (JSONObject)o1;
                 JSONObject js2 = (JSONObject)o2;
                 Integer i1 = Integer.valueOf(js1.get("autoId").toString());
                 Integer i2 = Integer.valueOf(js2.get("autoId").toString());
                 return i1-i2;
   }});

searchCondition.put("autoId", jsonArray.getJSONObject(jsonArray.size()-1).get("autoId"));
 

                            
                        
                    
                    
                    

你可能感兴趣的:(JSONArray 按某字段进行排序)