zoukankan      html  css  js  c++  java
  • jsonarray根据id排序

     List<JSONObject> jsonValue=new ArrayList<JSONObject>();
            JSONArray sortJsonarr=new JSONArray();
            for (int i = 0; i < jsonArray.length(); i++) {
                try {
                    jsonValue.add(jsonArray.getJSONObject(i));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            Collections.sort(jsonValue,new Comparator<JSONObject>() {
                private static final String key="id";
                public int compare(JSONObject a, JSONObject b) {
                Integer valA = 0;
                Integer valB = 0;
                try {
                    valA = a.getInt(key);
                    valB = b.getInt(key);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return valA.compareTo(valB);
            }
      });
    public JSONArray getAllServerType(int uid){
            String url = "";
            JSONArray jsonArray = this.giantBaseDaoInterface.getJsonObjectByUrl(url);
            JSONObject json =new JSONObject();
            try {
                json.put("id", "-1");
                json.put("text", "全部");
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonArray.put(json);
            //排序
            List<JSONObject> jsonValue=new ArrayList<JSONObject>();
            JSONArray sortJsonarr=new JSONArray();
            for (int i = 0; i < jsonArray.length(); i++) {
                try {
                    jsonValue.add(jsonArray.getJSONObject(i));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            Collections.sort(jsonValue,new Comparator<JSONObject>() {
                private static final String key="id";
                public int compare(JSONObject a, JSONObject b) {
                Integer valA = 0;
                Integer valB = 0;
                try {
                    valA = a.getInt(key);
                    valB = b.getInt(key);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return valA.compareTo(valB);
            }
            });
            for(int i=0;i<jsonArray.length();i++){
            sortJsonarr.put(jsonValue.get(i));
            }
            return sortJsonarr;
        }
     
  • 相关阅读:
    关于 <customErrors> 标记的“mode”属性设置为“Off”的问题的解决方案
    ASP.NET MVC的帮助类HtmlHelper和UrlHelper
    js判断手机浏览器操作系统和微信浏览器的方法
    javascript倒计时代码及倒计时弹窗
    修改UISearchBar背景色
    dispatch_group_async
    Block 代替for循环
    GCD 延时操作
    GCD 倒计时
    创建UIButton
  • 原文地址:https://www.cnblogs.com/raitorei/p/10550586.html
Copyright © 2011-2022 走看看