Java Android - 如何根据键对 JSONArray 进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31828574/
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
Java Android - How to sort a JSONArray based on keys
提问by Jhonatan Sandoval
I'm getting this string (from a webservice) into a JSONArray like this,
我将这个字符串(来自网络服务)放入这样的 JSONArray 中,
[
{
"lat": "-16.408545",
"lon": "-71.539105",
"type": "0",
"distance": "0.54"
},
{
"lat": "-16.4244317845",
"lon": "-71.52562186",
"type": "1",
"distance": "1.87"
},
{
"lat": "-16.4244317845",
"lon": "-71.52562186",
"type": "1",
"distance": "0.22"
}
]
I need to sort it by the distancekey to show the nearest first and farthest last. I didn't try any code because I really don't have any ideas. I'm not using the GSON library, I'm using org.json.JSONArray
.
我需要按距离键对其进行排序以显示最近的第一个和最远的最后一个。我没有尝试任何代码,因为我真的没有任何想法。我没有使用 GSON 库,我使用的是org.json.JSONArray
.
回答by crysis
First parse your array in a list
首先在列表中解析您的数组
JSONArray sortedJsonArray = new JSONArray();
List<JSONObject> jsonList = new ArrayList<JSONObject>();
for (int i = 0; i < jsonArray.length(); i++) {
jsonList.add(jsonArray.getJSONObject(i));
}
then use collection.sort to sort the newly created list
然后使用 collection.sort 对新创建的列表进行排序
Collections.sort( jsonList, new Comparator<JSONObject>() {
public int compare(JSONObject a, JSONObject b) {
String valA = new String();
String valB = new String();
try {
valA = (String) a.get("distance");
valB = (String) b.get("distance");
}
catch (JSONException e) {
//do something
}
return valA.compareTo(valB);
}
});
Insert the sorted values in your array
在数组中插入排序的值
for (int i = 0; i < jsonArray.length(); i++) {
sortedJsonArray.put(jsonList.get(i));
}
回答by Nitesh
Parse your json object into a model say array-List and sort it using comparator.
将您的 json 对象解析为一个模型,例如 array-List 并使用比较器对其进行排序。
ArrayList<ClassObject> dataList = new ArrayList<String>();
JSONArray array = new JSONArray(json);
for(Object obj : jsonArray){
dataList.add(//your data model);
}
Please refer this link for sorting of array-list http://java2novice.com/java-collections-and-util/arraylist/sort-comparator/
请参阅此链接以对数组列表进行排序 http://java2novice.com/java-collections-and-util/arraylist/sort-comparator/
回答by Noman Rafique
Try this. It should work
试试这个。它应该工作
ArrayList<JSONObject> array = new ArrayList<JSONObject>();
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
try {
array.add(jsonArray.getJSONObject(i));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Collections.sort(array, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject lhs, JSONObject rhs) {
// TODO Auto-generated method stub
try {
return (lhs.getDouble("distance").compareTo(rhs.getDouble("distance")));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
});
And after this you can convert sorted ArrayList
array
into JSONArray
.
在此之后,您可以将 sorted 转换ArrayList
array
为JSONArray
.
JSONArray jsonArray = new JSONArray(array);
String jsonArrayStr = jsonArray.toString();