您的位置:首页 > 产品设计 > UI/UE

java中根据value对key进行排序

2014-03-13 10:05 447 查看
public ArrayList<String> getAppSort(){

this.getAppDataFromDB();

maps = this.sortByValue(maps,isOrder);

Iterator it = maps.entrySet().iterator();

while (it.hasNext()) {

Map.Entry pairs = (Map.Entry) it.next();

String key = (String)pairs.getKey();

sortByApp.add(key);

}

return sortByApp;

}

//按照value对key进行排序

//reverse = false 按照从小到大的顺序排序

//reverse = true 按照从大到小的顺序排序

private Map sortByValue(Map map, final boolean reverse) {

List list = new LinkedList(map.entrySet());

Collections .sort(list, new Comparator() {

public int compare(Object o1, Object o2) {

if (reverse) {

return -((Comparable) ((Map .Entry)o1).getValue())

.compareTo(((Map .Entry)o2).getValue());

}

return ((Comparable) ((Map .Entry)o1).getValue())

.compareTo(((Map .Entry)o2).getValue());

}

});

Map result = new LinkedHashMap();

for (Iterator it = list.iterator(); it.hasNext();) {

Map.Entry entry = (Map.Entry) it.next();

result.put(entry.getKey(), entry.getValue());

}

return result;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: