Java 排序数组列表<;HashMap<;字符串,字符串>&燃气轮机;使用价值

Java 排序数组列表<;HashMap<;字符串,字符串>&燃气轮机;使用价值,java,sorting,arraylist,Java,Sorting,Arraylist,我的代码中有Hashmap的ArrayList 我不知道应该使用哪种排序方法? 我的代码如下 arraylist = new ArrayList<HashMap<String, String>>(); // Retrieve JSON Objects from the given URL address jsonobject = JSONFunctions .getJSONfr

我的代码中有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;
    }
我想按距离分类 请给我一些建议,我应该使用什么方法 我对哈希映射的数组列表和数组列表感到困惑 如何使用collection.sort对数据进行排序


我想根据ArrayList中的值进行排序。

使用比较器对其进行排序

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);
比较器距离比较器=新比较器(){ @凌驾 公共int比较(HashMap o1、HashMap o2){ //获取距离并比较距离。 整数距离1=Integer.parseInt(o1.get(“距离”); 整数距离2=Integer.parseInt(o2.get(“距离”); 返回距离1.与(距离2)相比; } }; //然后使用collections.sort()对其进行排序。 Collections.sort(arrayList、distanceComparator);
Collections.sort(arrayList,(HashMap m1,HashMap m2)->
Integer.parseInt(m1.get(“距离”).compareTo(Integer.parseInt(m2.get(“距离”)));

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

你不能使用树映射并免费获得排序吗?我不熟悉树映射的概念,但我会研究一下为什么你要使用Hashmap的ArrayList?您可以直接使用对象的ArrayList,其中对象将属于一个类(称为Vinces maybe),具有两个变量->城市,距离。通过这种方式,您可以在Vincements类中使用比较器对ArrayList进行排序,并使用您可以使用的任何方式。或者,正如芭丝谢芭提到的,你可以使用TreeSet。
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);
Collections.sort(arrayList, (HashMap<String, String> m1, HashMap<String, String> m2) -> 
            Integer.parseInt(m1.get("distance").compareTo(Integer.parseInt(m2.get("distance")))));