Java中对jsonArray的排序,使⽤的是Gson 使⽤Gson对json解析字符串,转化为json对象.
先上代码: 下⾯是main⽅法⾥⾯的代码
package testJava;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
le.gson.JsonParr;
le.gson.JsonArray;
le.gson.JsonObject;
public class TestJava {
public static void main(String[] args) {
/
/ TODO Auto-generated method stub
jsoaArraySort();
}
public static void jsoaArraySort() {
String arrayData = "["
+ "{\"Name\":\"TVS\",\"No\":" + 202 + ",\"Length\":" + 23 + "},"
+ "{\"Name\":\"TVC\",\"No\":" + 203 + ",\"Length\":" + 14 + "},"
+ "{\"Name\":\"Wel\",\"No\":" + 345 + ",\"Length\":" + 35 + "},"
+ "{\"Name\":\"Sma\",\"No\":" + 678 + ",\"Length\":" + 45 + "},"
+ "{\"Name\":\"Sma\",\"No\":" + 136 + ",\"Length\":" + 15 + "},"
+ "{\"Name\":\"Cus\",\"No\":" + 257 + ",\"Length\":" + 17 + "},"
+ "{\"Name\":\"And\",\"No\":" + 678 + ",\"Length\":" + 16 + "},"
+ "{\"Name\":\"Roo\",\"No\":" + 136 + ",\"Length\":" + 56 + "}"
+"]";
JsonParr parr = new JsonParr();
JsonArray jsonArray = parr.par(arrayData).getAsJsonArray();
System.out.println("init jsonArray=" + String());
JsonArray sort_JsonArray = new JsonArray();
List<JsonObject> list = new ArrayList<JsonObject>();
JsonObject jsonObj = null;
for (int i = 0; i < jsonArray.size(); i++) {
jsonObj = (JsonObject) (i);
list.add(jsonObj);
}
总务科科长//这⾥最核⼼的地⽅就是SortComparator这个类新年诗词
//其中构造⽅法的参数:
//sortItem是要排序的jsonArray中⼀个元素, 这⾥我选择是Name, 也可以选择No或者是Length
//sortType是排序的类型, 有三种情况
// 1. 排序的元素对应的值是int,那么sortType = "int";
// 2. 排序的元素对应的值是string,那么sortType = "string";
// 3. 排序的元素对应的是是其他类型, 默认是不排序, (后⾯可以扩展)
//sortDire是排序的⽅向, 可以是asc或者desc, 默认是数据的原始⽅向(就是没有排序⽅向)
String sortItem = "Length";
String sortType = "int";
String sortDire = "desc";
Collections.sort(list, new SortComparator(sortItem, sortType, sortDire));
for (int i = 0; i < list.size(); i++) {
jsonObj = (i);
sort_JsonArray.add(jsonObj);
}
大学生调查报告System.out.println("after sort_JsonArray=" + String());
}
}
我不知道用英语怎么说下⾯给出SortComparator.java
package testJava;
import java.util.Comparator;
le.gson.JsonObject;
public class SortComparator implements Comparator<JsonObject> {
private String sortItem;
private String sortType;
private String sortDire;
public SortComparator(String sortItem, String sortType, String sortDire) {
this.sortItem = sortItem;
this.sortType = sortType;
this.sortDire = sortDire;
}
@Override
public int compare(JsonObject o1, JsonObject o2) {
String value1 = o1.getAsJsonObject().get(sortItem).getAsString();
String value2 = o2.getAsJsonObject().get(sortItem).getAsString();
if ("int".equalsIgnoreCa(this.sortType)) { // int sort
int int1 = Integer.parInt(value1);
int int2 = Integer.parInt(value2);
if ("asc".equalsIgnoreCa(this.sortDire)) {
return int1 - int2;
} el if ("desc".equalsIgnoreCa(this.sortDire)) {
return int2 - int1;
} el {
return 0;
}
} el if ("string".equalsIgnoreCa(this.sortType)) { // string sort if ("asc".equalsIgnoreCa(this.sortDire)) {
pareTo(value2);
} el if ("desc".equalsIgnoreCa(this.sortDire)) {
pareTo(value1);
} el {
return 0;
}
} el { // nothing sort
return 0;
}
}
}
测试的结果:
jsonArray的初始值如下:
jsonArray = [
{"Name":"TVS","No":202,"Length":23},
{"Name":"TVC","No":203,"Length":14},
{"Name":"Wel","No":345,"Length":35},
{"Name":"Sma","No":678,"Length":45},
{"Name":"Sma","No":136,"Length":15},
{"Name":"Cus","No":257,"Length":17},
{"Name":"And","No":678,"Length":16},
{"Name":"Roo","No":136,"Length":56}
];
下⾯是按照Name元素从⼩到达排序: SortComparator("Name", "string", "asc") after sort_JsonArray=[
老师对学生的评价
{"Name":"And","No":678,"Length":16},
梦见大火烧房子 {"Name":"Cus","No":257,"Length":17},
{"Name":"Roo","No":136,"Length":56},
{"Name":"Sma","No":678,"Length":45},
{"Name":"Sma","No":136,"Length":15},
{"Name":"TVC","No":203,"Length":14},
{"Name":"TVS","No":202,"Length":23},
{"Name":"Wel","No":345,"Length":35}
]
下⾯是按照Name元素从⼤到⼩排序: SortComparator("Name", "string", "desc") after sort_JsonArray=[
{"Name":"Wel","No":345,"Length":35},
{"Name":"TVS","No":202,"Length":23},
{"Name":"TVC","No":203,"Length":14},
{"Name":"Sma","No":678,"Length":45},
{"Name":"Sma","No":136,"Length":15},
{"Name":"Roo","No":136,"Length":56},
{"Name":"Cus","No":257,"Length":17},
{"Name":"And","No":678,"Length":16}
]现代化管理
下⾯是按照Length元素从⼩到⼤排序: SortComparator("Length", "int", "asc") after sort_JsonArray=[
{"Name":"TVC","No":203,"Length":14},
{"Name":"Sma","No":136,"Length":15},
{"Name":"And","No":678,"Length":16},
{"Name":"Cus","No":257,"Length":17},
{"Name":"TVS","No":202,"Length":23},
{"Name":"Wel","No":345,"Length":35},
{"Name":"Sma","No":678,"Length":45},
{"Name":"Roo","No":136,"Length":56}
]
下⾯是按照Length元素从⼤到⼩排序: SortComparator("Length", "int", "desc") after sort_JsonArray=[
{"Name":"Roo","No":136,"Length":56},
{"Name":"Sma","No":678,"Length":45},汽车壁纸
{"Name":"Wel","No":345,"Length":35},
{"Name":"TVS","No":202,"Length":23},
{"Name":"Cus","No":257,"Length":17}, {"Name":"And","No":678,"Length":16}, {"Name":"Sma","No":136,"Length":15}, {"Name":"TVC","No":203,"Length":14} ]