采用重写Collections.sort方法实现对 List中的HashMap排序。
ArrayList<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
Map<String, Object> map2 = new HashMap<String, Object>();
Map<String, Object> map3 = new HashMap<String, Object>();
map1.put("str","aaa");
map2.put("str","bbb");
map3.put("str","ccc");
list.add(map1);
list.add(map2);
list.add(map3);
Collections.sort(list, new Comparator<HashMap<String,Object>>(){
@Override
public int compare(
HashMap<String, Object> o1,
HashMap<String, Object> o2) {
String name1 = (String) o1.get("str");
String name2 = (String) o2.get("str");
return String.valueOf(name1).compareTo(name2);
/* 如果需要比较的为int类型数据,用以下方法*/
//String name1 = (Integer) o1.get("str");
//String name2 = (Integer) o2.get("str");
//return name1 - name2;
}
});