zoukankan      html  css  js  c++  java
  • Java中对JSONArray中的对象进行排序

    String jsonArrStr = "[ { "ID": "135", "Name": "Fargo Chan" },{ "ID": "432", "Name": "Aaron Luke" },{ "ID": "252", "Name": "Dilip Singh" }]";

    JSONArray jsonArr =JSONObject.parseArray(jsonArrStr); 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>() { //You can change "Name" with "ID" if you want to sort by ID private static final String KEY_NAME = "ID"; @Override public int compare(JSONObject a, JSONObject b) { String valA = new String(); String valB = new String(); try { valA = (String) a.get(KEY_NAME); valB = (String) b.get(KEY_NAME); } catch (JSONException e) { //do something } return valB.compareTo(valA); //if you want to change the sort order, simply use the following: //return -valA.compareTo(valB); } }); for (int i = 0; i < jsonArr.size(); i++) { sortedJsonArray.add(jsonValues.get(i)); }

      

  • 相关阅读:
    命令行jarsigner签字和解决找不到证书链错误
    ERROR ITMS-90034
    module.exports 和 exports
    php扩展包
    switch的使用
    debug安卓屏幕滑动会抖动
    react native编译报错
    使用iTerm2替代Mac自带Terminal终端
    编码转换
    git 操作远程 本地缓存删除
  • 原文地址:https://www.cnblogs.com/ampl/p/11377667.html
Copyright © 2011-2022 走看看