Java - 根据两个属性值对 JSONArray 进行排序
Java - sort JSONArray based on two attribute values
我有一个如下所示的 JSONArray,
JSONArray dataArray = new JSONArray();
dataArray = [
{
"name": "name1",
"row": 1,
"value": 20
},
{
"name": "name2",
"row": 1,
"value": 10
},
{
"name": "name3",
"row": 2,
"value": 10
},
{
"name": "name4",
"row": 3,
"value": 30
},
{
"name": "name5",
"row": 3,
"value": 10
}
]
我需要比较row
属性,如果相同,需要比较value
属性并对数组中的对象进行排序。
尝试使用 Java 比较器,但无法正常工作。有人可以帮忙吗?
for(int i = 0; i < dataArray.size(); i++) {
elementList.add((JSONObject) dataArray.get(i));
}
Long row1 = null;
for (JSONObject obj : elementList) {
if(row1 == null) {
row1 = (Long) ((JSONObject) obj.get("row"));
}
else {
Long row2 = (Long) ((JSONObject) obj.get("row"));
if(row2 == row1) {
//call the comparator, but how to pass two objects?
}
row1 = row2;
}
}
这里有一个更简单的方法:
您可以使用 Jackson 的 ObjectMapper
将 JSON 字符串转换为 List<YourObject>
List<YourObject> list = objectMapper.readValue(json, new TypeReference<List<YourObject>>(){});
然后使用Collections.sort
和Comparator
对这个列表进行排序。您还可以实施自定义 Comparator
以根据您的情况按多个属性对列表进行排序。
list.sort(Comparator.comparing(YourObject::getRow)
.thenComparing(YourObject::getValue));
扩展 this answer 以匹配您的场景会很容易
但不是
return valA.compareTo(valB);
你应该做的
int comp = valA.compareTo(valB);
if (comp == 0){
String valC = (String) a.get(KEY_NAME2);
String valD = (String) b.get(KEY_NAME2);
return valC.compareTo(valD);
} else {
return comp;
}
所以应该是下面的。
JSONArray sortedJsonArray = new JSONArray();
List<JSONObject> jsonValues = new ArrayList<JSONObject>();
for (int i = 0; i < dataArray.length(); i++) { // <---- dataArray is the input that you have
jsonValues.add(dataArray.getJSONObject(i));
}
Collections.sort( jsonValues, new Comparator<JSONObject>() {
//You can change "Name" with "ID" if you want to sort by ID
private static final String KEY_NAME1 = "row";
private static final String KEY_NAME2 = "value";
@Override
public int compare(JSONObject a, JSONObject b) {
String valA = new String();
String valB = new String();
try {
valA = (String) a.get(KEY_NAME1);
valB = (String) b.get(KEY_NAME1);
}
catch (JSONException e) {
//do something
}
int comp = valA.compareTo(valB);
if (comp == 0){
String valC = (String) a.get(KEY_NAME2);
String valD = (String) b.get(KEY_NAME2);
return valC.compareTo(valD);
} else {
return comp;
}
}
});
编辑: 修改为KEY_NAME1 = "row";
以符合新题要求
假设您将 json 数据反序列化为
这样的对象
public class DataRow {
private String name;
private int row;
private int value;
// Getter, Setter, Constructor what ever needed
}
然后您可以像这样使用列表和流:
List<DataRow> datarows = //... read data from json
List<DataRow> sorted = datarows.stream()
.sorted(Comparator.comparing(DataRow::getRow).thenComparingInt(DataRow::getValue)).toList();
我有一个如下所示的 JSONArray,
JSONArray dataArray = new JSONArray();
dataArray = [
{
"name": "name1",
"row": 1,
"value": 20
},
{
"name": "name2",
"row": 1,
"value": 10
},
{
"name": "name3",
"row": 2,
"value": 10
},
{
"name": "name4",
"row": 3,
"value": 30
},
{
"name": "name5",
"row": 3,
"value": 10
}
]
我需要比较row
属性,如果相同,需要比较value
属性并对数组中的对象进行排序。
尝试使用 Java 比较器,但无法正常工作。有人可以帮忙吗?
for(int i = 0; i < dataArray.size(); i++) {
elementList.add((JSONObject) dataArray.get(i));
}
Long row1 = null;
for (JSONObject obj : elementList) {
if(row1 == null) {
row1 = (Long) ((JSONObject) obj.get("row"));
}
else {
Long row2 = (Long) ((JSONObject) obj.get("row"));
if(row2 == row1) {
//call the comparator, but how to pass two objects?
}
row1 = row2;
}
}
这里有一个更简单的方法:
您可以使用 Jackson 的 ObjectMapper
List<YourObject>
List<YourObject> list = objectMapper.readValue(json, new TypeReference<List<YourObject>>(){});
然后使用Collections.sort
和Comparator
对这个列表进行排序。您还可以实施自定义 Comparator
以根据您的情况按多个属性对列表进行排序。
list.sort(Comparator.comparing(YourObject::getRow)
.thenComparing(YourObject::getValue));
扩展 this answer 以匹配您的场景会很容易
但不是
return valA.compareTo(valB);
你应该做的
int comp = valA.compareTo(valB);
if (comp == 0){
String valC = (String) a.get(KEY_NAME2);
String valD = (String) b.get(KEY_NAME2);
return valC.compareTo(valD);
} else {
return comp;
}
所以应该是下面的。
JSONArray sortedJsonArray = new JSONArray();
List<JSONObject> jsonValues = new ArrayList<JSONObject>();
for (int i = 0; i < dataArray.length(); i++) { // <---- dataArray is the input that you have
jsonValues.add(dataArray.getJSONObject(i));
}
Collections.sort( jsonValues, new Comparator<JSONObject>() {
//You can change "Name" with "ID" if you want to sort by ID
private static final String KEY_NAME1 = "row";
private static final String KEY_NAME2 = "value";
@Override
public int compare(JSONObject a, JSONObject b) {
String valA = new String();
String valB = new String();
try {
valA = (String) a.get(KEY_NAME1);
valB = (String) b.get(KEY_NAME1);
}
catch (JSONException e) {
//do something
}
int comp = valA.compareTo(valB);
if (comp == 0){
String valC = (String) a.get(KEY_NAME2);
String valD = (String) b.get(KEY_NAME2);
return valC.compareTo(valD);
} else {
return comp;
}
}
});
编辑: 修改为KEY_NAME1 = "row";
以符合新题要求
假设您将 json 数据反序列化为
这样的对象public class DataRow {
private String name;
private int row;
private int value;
// Getter, Setter, Constructor what ever needed
}
然后您可以像这样使用列表和流:
List<DataRow> datarows = //... read data from json
List<DataRow> sorted = datarows.stream()
.sorted(Comparator.comparing(DataRow::getRow).thenComparingInt(DataRow::getValue)).toList();