gpt4 book ai didi

java - 按降序对 jsonarray 数据进行排序

转载 作者:行者123 更新时间:2023-12-02 00:36:08 25 4
gpt4 key购买 nike

[
{
"id":"1",
"created_at":"2019-08-19 02:54:36",
"updated_at":"2019-09-04 15:00:05"
},
{
"id":"2",
"created_at":"2019-08-27 08:59:18",
"updated_at":"2019-09-04 14:59:14"
},
{
"id":"4",
"created_at":"2019-08-29 20:19:54",
"updated_at":"2019-09-04 14:58:53"
}
]

如何根据“created_at”(2019-08-30,2019-08-29)数据按降序对json数据进行排序,并将值设置为android中的textview。

最佳答案

请尝试一下

public static JSONArray sortJsonArray(JSONArray array) {
List<JSONObject> jsons = new ArrayList<JSONObject>();
for (int i = 0; i < array.length(); i++) {
jsons.add(array.getJSONObject(i));
}
Collections.sort(jsons, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject lhs, JSONObject rhs) {
String lid = lhs.getString("created_at");
String rid = rhs.getString("created_at");
// Here you could parse string id to integer and then compare.
return lid.compareTo(rid);
}
});
return new JSONArray(jsons);
}

关于java - 按降序对 jsonarray 数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57971738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com