第一步:Map中新增sort临时键
// 初始化Map集合
List> columns = new ArrayList>();
Map c1 = new HashMap();
c1.put("sort", "8");
c1.put("title", "www.chuweibiao.com");
columns.add(c1);
Map c2 = new HashMap();
c2.put("sort", "5");
c2.put("title", "www.chuweibiao.com");
columns.add(c2);
Map c3 = new HashMap();
c3.put("sort", "13");
c3.put("title", "www.chuweibiao.com");
columns.add(c3);
第二步:进行排序
/**
* Map类型元素集合排序
* @param columns
* Map类型元素集合
*/
private void listSortingForMapTypeElement(List> columns) {
Collections.sort(columns, new Comparator>() {
public int compare(Map last, Map next) {
Object lastSort = last.get("sort");
Object nextSort = next.get("sort");
if (lastSort == null || nextSort == null) {
return 0;
} else {
return Integer.parseInt(String.valueOf(lastSort))
> Integer.parseInt(String.valueOf(nextSort)) ? 1 : 0;
}
}
});
}
第三步:移除临时键
/**
* 移除排序临时键
* @param columns
* Map类型元素集合
*/
private void removeSortKey(List> columns) {
for (Map column : columns) {
column.remove("sort");
}
}
谁有更好的办法请拍砖指教,O(∩_∩)O谢谢~
原文:http://blog.csdn.net/for_china2012/article/details/42645985