JSONArray排序和倒序

JSONArray排序

// JSONArray转list
List<JSONObject> list = JSONArray.parseArray(ordersDataArray.toJSONString(), JSONObject.class);
Collections.sort(list, new Comparator<JSONObject>() {
    
    @Override
    public int compare(JSONObject o1, JSONObject o2) {
        long a = o1.getLongValue("time");
        long b = o2.getLongValue("time");
        if (a < b) { // 降序
            return 1;
        } else if(a == b) {
            return 0;
        } else
            return -1;
        }
    
});

// list转JSONArray
JSONArray jsonArray = JSONArray.parseArray(list.toString());

JSONArray倒序

// dataArray倒序
// JSONArray转list
List<JSONObject> list = JSONArray.parseArray(dataArray.toJSONString(), JSONObject.class);
Collections.reverse(list);
// list转JSONArray
JSONArray _dataArray = JSONArray.parseArray(list.toString());

自己留档。。。。

猜你喜欢

转载自blog.csdn.net/qq_41992943/article/details/105273151