JavaScript通过key对map值进行排序

javascript 17-07-11 17:16 2816  

> 我们知道map是无序的,想对map进行排序,就得进行相应的转化。 ``` function sortMap(){ var hashmap = {"2":"javascript","1":"java","5":"golang","8":"lua"}; console.log(hashmap); var keys = new Array(); for(var key in hashmap){ keys.push(key); } keys.sort(function(a,b){return b-a}); for(var index in keys){ var key = keys[index]; console.log(hashmap[key]); } } ```