对jsonarray中某个对象的某一个字段进行排序

package com.ysj.control;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class SortTest {


public static void main(String[] args) {
// TODO Auto-generated method stub
String result="[{\"RANGE_area\": \"[0,15]\", \"city\": \"长沙市\", \"county\":\"天心区\",\"SUM_area\": 100,\"AVG_length\": 10 },"
+"{ \"RANGE_area\": \"(15,30]\", \"city\": \"长沙市\",\"county\":\"天心区\",\"SUM_area\": 200,\"AVG_length\": 10 },"
+"{ \"RANGE_area\": \"(30,60]\", \"city\": \"长沙市\", \"county\": \"天心区\",\"SUM_area\": 200, \"AVG_length\": 10 },"
+ "{\"RANGE_area\":\"[0,15]\", \"city\": \"长沙市\", \"county\": \"开福区\", \"SUM_area\": 200, \"AVG_length\": 10 },"
+ "{\"RANGE_area\": \"(15,30]\", \"city\": \"长沙市\", \"county\": \"开福区\", \"SUM_area\": 500, \"AVG_length\": 10 },"
+ "{ \"RANGE_area\": \"[0,10]\", \"city\": \"衡阳市\", \"county\": \"衡阳县\", \"SUM_area\": 200, \"AVG_length\": 10 },"
+ "{ \"RANGE_area\": \"(30,60]\", \"city\": \"衡阳市\", \"county\": \"衡阳县\", \"SUM_area\": 200, \"AVG_length\": 10 },"
+ "{ \"RANGE_area\": \"(15,30]\", \"city\": \"衡阳市\", \"county\": \"祁东县\", \"SUM_area\": 200, \"AVG_length\": 10 },"
+ "{ \"RANGE_area\": \"(30,60]\", \"city\": \"株洲市\", \"county\": \"醴陵市\", \"SUM_area\": 80, \"AVG_length\": 10 },"
+ "{ \"RANGE_area\": \"[0,10]\", \"city\": \"湘潭市\", \"county\": \"茶陵市\", \"SUM_area\": 200, \"AVG_length\": 10 },]";
JSONArray arr=JSON.parseArray(result);
JSONArray sortJsonarr=new JSONArray();
List jsonValue=new ArrayList();
for(int i=0;i jsonValue.add(arr.getJSONObject(i));
}
Collections.sort(jsonValue,new Comparator() {
// private static final String key="city";
private static final String key="SUM_area";
public int compare(JSONObject a, JSONObject b) {
// String valA=a.getString(key);
// String valB=b.getString(key);
Integer valA=a.getIntValue(key);
Integer valB=b.getIntValue(key);
return valA.compareTo(valB);
}
});
for(int i=0;i sortJsonarr.add(jsonValue.get(i));
}
System.out.println(sortJsonarr.toJSONString());
}


}

你可能感兴趣的:(对jsonarray中某个对象的某一个字段进行排序)