我进行了很多搜索,但没有找到适合我的问题的解决方案。现在我想对 ArrayList<Hashmap<String, Object>>
进行排序根据 int 值。
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
保存了很多这样的Hashmap,我想根据distance
对它们进行排序值,那么有什么办法可以做到这一点吗?非常感谢。
最佳答案
如果我正确理解您的用例,您应该更改您的数据表示形式。
创建一个名为 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()
应该可以。
关于java - 按 int 值对 Hashmap<String,Object> 的 ArrayList 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16268048/