通过int值</string,object>对Hashmap <string,object>的ArrayList进行排序

时间:2013-04-28 21:23:02

标签: java sorting hashmap

我一直在搜索很多,但我找不到合适的解决方案来解决我的问题。现在我想根据int值对ArrayList<Hashmap<String, Object>>进行排序。

HashMap<String, Object>map = new HashMap<String, Object>();
map.put("friend", "x");
map.put("status", "yyoyo");
map.put("time", "20:10");
map.put("lat", 30.030535);
map.put("lng", 31.506050);
map.put("distance", 50); 
statusMapList.add(map);

现在,这是一个示例。 statusMapList包含这样的许多Hashmaps,我想根据distance值对它们进行排序,有没有办法做到这一点?非常感谢。

2 个答案:

答案 0 :(得分:2)

如果我正确理解您的用例,您应该更改数据表示。

创建一个名为Status的类,实现Comparable

public class Status implements Comparable<Status>
{
 String friend;
 String status;
 String time; // Or DateTime
 double lat;
 double lng;
 double distance;

// Override compareTo and use distance to decide the order
// Override equals
// Override hashcode
}

现在您应该创建一个ArrayList<Status>(),然后Collections.sort()应该有效。

答案 1 :(得分:1)

只需撰写您自己的Comparator<HashMap<String, Object>>并使用Collections.sort()的{​​{3}}。