如下
- //转成list
- List<JSONObject> list = new ArrayList<JSONObject> ();
- JSONObject jsonObj = null;
- for (int i = 0; i < jsonArr.size(); i++) {
- jsonObj = (JSONObject)jsonArr.get(i);
- list.add(jsonObj);
- }
- Collections.sort(list,new MyComparator());
- jsonArr.clear();
- for (int i = 0; i < list.size(); i++) {
- jsonObj = list.get(i);
- jsonArr.add(jsonObj);
- }
- //jsonArr排序
MyComparator.java
compare 不是返回的1/0.返回的是两次比较的差值。
- public class MyComparator implements Comparator<JSONObject> {
-
- @Override
- public int compare(JSONObject o1,JSONObject o2) {
- String key1 = o1.getString("id");
- String key2 = o2.getString("id");
- int int1 = Integer.parseInt(key1);
- int int2 = Integer.parseInt(key2);
-
- /* if(int1>int2){ return 1; } return 0; */
- //2017年8月17日14:09:30修改。
- return int1-int2;
- }
-
-
- }