java 如何在android中对JSONArray进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17697568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort JSONArray in android
提问by M.A.Murali
How to sort JSONArray by its Tag name with ascending and descending on android.
如何在 android 上按升序和降序按标签名称对 JSONArray 进行排序。
In my application i have a JSON like the following and need to display the data according to users option(sorting by user_id tag in ascending or descending order). I have parsed the JSON as the following:
在我的应用程序中,我有一个如下所示的 JSON,需要根据用户选项显示数据(按 user_id 标签按升序或降序排序)。我已将 JSON 解析如下:
JSONObject mObject = new JSONObject(JSONResponse);
String commentsVal = mObject.getString("message");
String resultVal = mObject.getString("result");
JSONArray resultArray = new JSONArray(resultVal);
int resultSize = resultArray.length();
for (int i = 0; i < resultArray.length(); i++) {
JSONObject resultObj = resultArray.getJSONObject(i);
resultObj.getString("comment_id");
.....
}
..............
and this is my JSON response:
这是我的 JSON 响应:
{
"success": 1,
"message": "some message",
"result": [
{
"comment_id": "43906",
"comp_id": "116725",
"user_id": "48322",
"agree": "0",
.....
"replies": [....]
},
{
"comment_id": "43905",
"comp_id": "116725",
"user_id": "48248",
"agree": "0",
.......
"replies": [...]
}
]
}
I need the "result" JSON Array sorted by the "user_id" tag name while parsing, how to do it in Android?
我需要在解析时按“user_id”标签名称排序的“结果”JSON数组,如何在Android中做到这一点?
回答by faylon
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("comment_id");
String rid = rhs.getString("comment_id");
// Here you could parse string id to integer and then compare.
return lid.compareTo(rid);
}
});
return new JSONArray(jsons);
}
This piece of code could return a sorted JSONArray, but i still recommend you to parse JSONObject to your own data beans, and then do the sorting on them.
这段代码可以返回一个排序的 JSONArray,但我仍然建议您将 JSONObject 解析为您自己的数据 bean,然后对它们进行排序。
回答by Matt Taylor
It seems to me like the easiest way to do it, rather than "sorting the JSON Array" as you suggest, is to just parse the whole thing into a data structure which contains the information like so:
在我看来,最简单的方法是将整个事物解析为包含如下信息的数据结构,而不是像您建议的那样“对 JSON 数组进行排序”:
class Results {
String user_id;
Strung comment_id;
}
Save this as an ArrayList or a [insert favourite List structure].
将其另存为 ArrayList 或 [插入最喜欢的列表结构]。
Once you've parsed the entire JSON Array, you can then sort your ArrayList based on the user_id field using Collections.sort
, giving you them in order
一旦你解析了整个 JSON 数组,你就可以根据 user_id 字段对你的 ArrayList 使用 排序Collections.sort
,按顺序给你
回答by Chintan Rathod
Following code may useful to you.
以下代码可能对您有用。
ArrayList<ResultBean> user_array;
Collections.sort(user_array, new Comparator<ResultBean>() {
@Override
public int compare(ResultBean data1, ResultBean data2) {
if (data1.getUserId() >= data2.getUserId()) {
return -1;
} else {
return 1;
}
}
});
Your ResultBean
will look like following.
您ResultBean
将如下所示。
public class ResultBean {
//other methods and declarations...
public int getUserId() {
return userId;
}
}