如何在android上订购多列Hashmap <string> <string>?</string> </string>

时间:2014-11-17 09:42:27

标签: java android hashmap

任何人都知道如何按特定列排序哈希图&#34;距离&#34;?

double distanceInMeters = location.distanceTo(location2);
DecimalFormat doubleform = new DecimalFormat("0.0");

HashMap<String,String> map = new HashMap<String, String>();
map.put("id", cursor.getString(cursor.getColumnIndex("id_ret")));
map.put("nome", cursor.getString(cursor.getColumnIndex("nome")));
map.put("endereco", cursor.getString(cursor.getColumnIndex("endereco")));
map.put("bairro", cursor.getString(cursor.getColumnIndex("bairro")));
map.put("cidade", cursor.getString(cursor.getColumnIndex("cidade")));
map.put("uf", cursor.getString(cursor.getColumnIndex("uf")));
map.put("distance", doubleform.format(distanceInMeters/1000)); <= Need to order this column!
list.add(map);

谢谢!

1 个答案:

答案 0 :(得分:0)

按路线类,我的意思如下。

public class Route implements Comparable<Route> {

    private String city;
    private String state;
    private String distance; 
    // add other fields that you require

    //constructor and/or static methods


    public String getDistance() {
        return distance;
    }

    // other getters

    @Override
    public int compareTo(Route o) {
        // Teach your compareTo method to compare routes by distance. 
        // Might involve comparing the double types.
    }
    // Add equals and hashCode methods too that are consistent with the
    // compareTo method. Add toString method also.



}

然后我会读取这些列值并创建我的Route对象并将它们放在List中,如下所示。

List<Route> routes = new ArrayList<>();
routes.add(new Route(.....));
Collections.sort(routes);// this will sort the routes.

我们正在使用List,因为您可以多次使用相同距离的路径。