// 项目场景说明: 对于运单的常跑进行显示,前端页面显示5个常跑地址
public class MapSortUtils {
/**
* @冒泡排序(降序)
* @param map
* @return
*/
public static LinkedHashMap mapSort( Map map){
LinkedHashMap linkedHashMap=new LinkedHashMap<>();
List> list=new ArrayList>(map.size());
list.addAll(map.entrySet());
int num=map.size();
for(int i=0;i) {
for(int j=0;j) {
Entry e1=list.get(j);
Entry e2=list.get(j+1);
if(e1.getValue()<e2.getValue()) {
Collections.swap(list,j, j+1);
}
}
}
for(int n=0;n<=num-1;n++) {
Entry entry=list.get(n);
linkedHashMap.put(entry.getKey(), entry.getValue());
}
return linkedHashMap;
}
public static void main(String[] args) {
Map map=new HashMap();
map.put("小明1", 1);
map.put("小明2", 11);
map.put("小明3", 12);
map.put("小明4", 9);
map.put("小明5", 8);
LinkedHashMap hashmap=mapSort(map);
for(String key:hashmap.keySet()) {
System.out.println("key:"+key+";value:"+map.get(key));
}
}
}