zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    Delphi对象的产生和消亡过程
    WIN32的时空观
    PHP类的用法
    D7的System.pas单元的实现部分
    PHP的最简单用法
    C调用Lua
    js连连看
    动态属性的一个架构
    Entity Framework开源了
    apachesolr4.0.0ALPHA中文分析器IKAnalyzer4.0
  • 原文地址:https://www.cnblogs.com/yongguang1990/p/10249190.html
Copyright © 2011-2022 走看看