记录一下
/** *@Author:vic *@Date:14:59 2018/11/30 *@Description: is_desc-false升序列 is_desc-true降序 */ public static String jsonArraySort(JSONArray jsonArr,String sortKey,boolean is_desc) { JSONArray sortedJsonArray = new JSONArray(); List<JSONObject> jsonValues = new ArrayList<JSONObject>(); for (int i = 0; i < jsonArr.size(); i++) { jsonValues.add(jsonArr.getJSONObject(i)); } Collections.sort(jsonValues, new Comparator<JSONObject>() { private final String KEY_NAME = sortKey; @Override public int compare(JSONObject a, JSONObject b) { String valA = new String(); String valB = new String(); try { valA = a.getString(KEY_NAME); valB = b.getString(KEY_NAME); } catch (JSONException e) { e.printStackTrace(); } if (is_desc){ return -valA.compareTo(valB); } else { return -valB.compareTo(valA); } } }); for (int i = 0; i < jsonArr.size(); i++) { sortedJsonArray.add(jsonValues.get(i)); } return sortedJsonArray.toString(); }