• JSONArray排序


    JSONArray根据某个key进行排序,注意key的类型。

    1、只支持JDK1.8以上版本

     private static JSONArray sortProxyAndCdn(JSONArray bindArrayResult) {
            System.out.println("排序前:"+bindArrayResult);
            bindArrayResult.sort(Comparator.comparing(obj -> ((JSONObject) obj).getString("cdn").length()).reversed());
          System.out.println("排序后:" + bindArrayResult);
          return bindArrayResult;
        }

     2、通用版本

     private static JSONArray sortProxyAndCdn(JSONArray bindArrayResult) {
          List<JSONObject> list = JSONArray.parseArray(bindArrayResult.toJSONString(), JSONObject.class);
          System.out.println("排序前:"+bindArrayResult);
          Collections.sort(list, new Comparator<JSONObject>() {
              @Override
              public int compare(JSONObject o1, JSONObject o2) {
                  int a = o1.getString("cdn").length();
                  int b = o2.getString("cdn").length();
                  if (a > b) {
                      return -1;
                  } else if(a == b) {
                      return 0;
                  } else
                      return 1;
                  }
          });
          JSONArray jsonArray = JSONArray.parseArray(list.toString());
          System.out.println("排序后:"+jsonArray);
          return jsonArray;
        }
  • 相关阅读:
    RSA加密算法
    ios 经典错误
    C--指针函数,static
    svn---命令行操作
    iOS中的自由桥接
    ios--socket
    ios错误修改了系统头文件
    ios数据库FMDB
    CoreDate的使用
    ios简单数据库运用
  • 原文地址:https://www.cnblogs.com/yongguang1990/p/10249190.html
Copyright © 2020-2023  润新知