我有我的代碼中的散列圖的ArrayList 我有混淆,我應該使用哪種排序方法? 我的代碼如下排序ArrayList <HashMap <字符串,字符串>>使用值
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONFunctions
.getJSONfromURL("https://api.foursquare.com/v2/venues/search? client_id=ACAO2JPKM1MXHQJCK45IIFKRFR2ZVL0QASMCBCG5NPJQWF2G&client_secret=YZCKUYJ1W HUV2QICBXUBEILZI1DMPUIDP5SHV043O04FKBHL&v=20130815&ll=-34.678,138.87&radius=5000§ion=coffee");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONObject("response");
JSONArray jsonSubarray = jsonarray.getJSONArray("venues");
Log.v("Response Array", String.valueOf(jsonSubarray));
for (int i = 0; i < jsonSubarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonSubarray.getJSONObject(i);
// Retrive JSON Objects
// Retrive JSON Objects
Log.v("Store name", jsonobject.getString("name"));
map.put("storeName", jsonobject.getString("name"));
JSONObject locationObject = jsonobject.getJSONObject("location");
Log.v("if condition",String.valueOf(locationObject.has("city")));
if(locationObject.has("city"))
{
map.put("city", locationObject.getString("city"));
}
else
map.put("city","N/A");
double km = Double.valueOf(locationObject.getString("distance"))/1000 ;
Log.v("distance", String.valueOf(km));
map.put("distance", String.valueOf(km));
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
這裏是我的JSON數據格式
"id": "4e321829d4c00077f657b8ec",
"name": "TeAro Estate Wines",
"contact": {},
"location": {
"lat": -34.670859098532766,
"lng": 138.89067804714347,
"distance": 2053,
"cc": "AU",
"city": "Williamstown",
"state": "SA",
"country": "Australia",
"formattedAddress": [
"Williamstown SA",
"Australia"
]
},
"categories": [
{
"id": "4bf58dd8d48988d14b941735",
"name": "Winery",
"pluralName": "Wineries",
"shortName": "Winery",
"icon": {
"prefix": "https://ss3.4sqi.net/img/categories_v2/food/winery_",
"suffix": ".png"
},
"primary": true
}
],
{
"id": "4c454825f0bdd13a65c9cacc",
"name": "Barossa Dam",
"contact": {},
"location": {
"address": "Dam it",
"lat": -34.645353,
"lng": 138.848115,
"distance": 4150,
"cc": "AU",
"city": "Sandy Creek",
"state": "SA",
"country": "Australia",
"formattedAddress": [
"Dam it",
"Sandy Creek SA",
"Australia"
]
},
我想距離 對它進行排序,請給我一些建議,我應該用什麼方法 我弄糊塗數組列表和哈希映射數組列表 如何使用collection.sort對數據進行排序。
我想根據ArrayList中的值進行排序。
你不能使用TreeMap並獲得免費的排序嗎? – Bathsheba
我不熟悉TreeMap的概念,但我會研究它 –
爲什麼使用Hashmap的ArrayList?您可以直接使用ObjectList的ArrayList,其中Object將是一個類(稱爲場地),並帶有兩個變量 - > City,Distance。這樣,您可以使用Venues類中的Comparator對ArrayList進行排序。否則,正如Bathsheba所說,你可以使用TreeSet。 –