import java.util.*;
public class java_ListMapSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Map> list = new ArrayList<Map>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("name", "p");
map1.put("cj", "5");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("name", "h");
map2.put("cj", "12");
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("name", "f");
map3.put("cj", "31");
list.add(map1);
list.add(map3);
list.add(map2);
//排序前
for (Map<String, Object> map : list) {
System.out.println(map.get("cj"));
}
Collections.sort(list, new Comparator<Map>() {
public int compare(Map o1, Map o2) {
Integer name1 = Integer.valueOf(o1.get("cj").toString()) ;//name1是从你list里面拿出来的一个
Integer name2 = Integer.valueOf(o2.get("cj").toString()) ; //name1是从你list里面拿出来的第二个name
return name1.compareTo(name2);
}
});
//排序后
System.out.println("-------------------");
for (Map<String, Object> map : list) {
System.out.println(map.get("cj"));
}
}
}
升序排序前
5
31
12
-------------------
排序后
5
12
31