【问题标题】:sort ArrayList<HashMap<String, String>> using value使用值对 ArrayList<HashMap<String, String>> 进行排序
【发布时间】:2015-07-10 13:57:25
【问题描述】:

我的代码中有 Hashmap 的 ArrayList 我对我应该使用哪种排序方法感到困惑? 我的代码如下

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&section=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 并免费进行排序吗?
  • 我不熟悉 TreeMap 的概念,但我会研究一下
  • 为什么要用Hashmap的ArrayList?您可以直接使用 Object 的 ArrayList,其中 Object 将属于具有两个变量 -> City、Distance 的类(可能称为场所)。这样,您可以使用 Venues 类中的 Comparator 对 ArrayList 进行排序。或者,正如 Bathsheba 所说,您可以使用 TreeSet。

标签: java sorting arraylist


【解决方案1】:

使用比较器对其进行排序。

Comparator<HashMap<String, String>> distanceComparator = new Comparator<HashMap<String,String>>() {

    @Override
    public int compare(HashMap<String, String> o1, HashMap<String, String> o2) {
        // Get the distance and compare the distance.
        Integer distance1 = Integer.parseInt(o1.get("distance"));
        Integer distance2 = Integer.parseInt(o2.get("distance"));

        return distance1.compareTo(distance2);
    }
};

// And then sort it using collections.sort().
Collections.sort(arrayList, distanceComparator);

【讨论】:

    【解决方案2】:
    Collections.sort(arrayList, (HashMap<String, String> m1, HashMap<String, String> m2) -> 
                Integer.parseInt(m1.get("distance").compareTo(Integer.parseInt(m2.get("distance")))));
    

    使用 java 1.8 和 Lambda 表达式功能是一种更简洁的想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2017-06-01
      • 1970-01-01
      • 2014-03-01
      相关资源
      最近更新 更多