Javascript 如何在javascript中按值对地图进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37982476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort a map by value in javascript?
提问by chenxinlong
var map = new Map();
map.set("orange",10);
map.set("apple",5);
map.set("banana",20);
map.set("cherry",13);
How to sort this map by value?
如何按值对这张地图进行排序?
采纳答案by Nina Scholz
Yo could take a different approach and change Symbol.iterator
of Map.prototype[@@iterator]()
for a custom sorted result.
哟可以采取不同的方法和变化Symbol.iterator
的Map.prototype[@@iterator]()
自定义排序结果。
var map = new Map();
map.set("orange", 10);
map.set("apple", 5);
map.set("banana", 20);
map.set("cherry", 13);
map[Symbol.iterator] = function* () {
yield* [...this.entries()].sort((a, b) => a[1] - b[1]);
}
for (let [key, value] of map) { // get data sorted
console.log(key + ' ' + value);
}
console.log([...map]); // sorted order
console.log([...map.entries()]); // original insertation order
.as-console-wrapper { max-height: 100% !important; top: 0; }
回答by Miroslav Savovski
const myMap = new Map();
myMap.set("a",3);
myMap.set("c",4);
myMap.set("b",1);
myMap.set("d",2);
// sort by value
const mapSort1 = new Map([...myMap.entries()].sort((a, b) => b[1] - a[1]));
console.log(mapSort1);
// Map(4)?{"c" => 4, "a" => 3, "d" => 2, "b" => 1}
const mapSort2 = new Map([...myMap.entries()].sort((a, b) => a[1] - b[1]));
console.log(mapSort2);
// Map(4)?{"b" => 1, "d" => 2, "a" => 3, "c" => 4}
// sort by key
const mapSort3 = new Map([...myMap.entries()].sort());
console.log(mapSort3);
// Map(4)?{"a" => 3, "b" => 1, "c" => 4, "d" => 2}
const mapSort4 = new Map([...myMap.entries()].reverse());
console.log(mapSort4);
// Map(4)?{"d" => 2, "b" => 1, "c" => 4, "a" => 3}
回答by newguy
In ES6 you can do it like this: (assume your Map object is m
).
在 ES6 中,您可以这样做:(假设您的 Map 对象是m
)。
[...m].map(e =>{ return e[1];}).slice().sort(function(a, b) {
return a - b;
});
the spread operator turns a Map object into an array, then takes out the second element of each subarray to build a new array, then sort it. If you want to sort in descending order just replace a - b
with b - a
.
展开运算符将 Map 对象转换为数组,然后取出每个子数组的第二个元素构建新数组,然后对其进行排序。如果您想按降序排序,只需替换a - b
为b - a
.
回答by Nguy?n Th?ng
You can use list maps instead of map only. Try this:
您可以使用列表地图而不是仅地图。尝试这个:
var yourListMaps = [];
var a = {quantity: 10, otherAttr: 'tmp1'};
var b = {quantity: 20, otherAttr: 'tmp2'};
var c = {quantity: 30, otherAttr: 'tmp3'};
yourListMaps.push(a);
yourListMaps.push(b);
yourListMaps.push(c);
And if you want to sort by quantity, you can:
如果您想按数量排序,您可以:
// Sort c > b > a
yourListMaps.sort(function(a,b){
return b.quantity - a.quantity;
});
or
或者
// Sort a > b > c
yourListMaps.sort(function(a,b){
return a.quantity - b.quantity;
});
回答by zapper123
It is a key-value mapping. Maintaining order in which keys are inserted defeats the benefit of O(1) insertion time.
它是一个键值映射。保持插入键的顺序会破坏 O(1) 插入时间的好处。
Why would you want to sort it? If required its better to maintain an array of key-value pairs and sort them to use later. For your case,
为什么要排序?如果需要,最好维护一个键值对数组并对它们进行排序以备后用。对于你的情况,
arr = [["orange", 10],["appple", 5], ["banana", 20], ["cherry", 13]];
Use a custom sort function as
使用自定义排序函数作为
arr.sort = function(a,b) {
return a[1]>b[1]? 1:a[1]<b[1]?-1:0;
}
Apply sort,
应用排序,
keysArr.sort()
Traverse sorted keysArr and use elements that as a key to retrieve value.
遍历已排序的 keysArr 并使用元素作为键来检索值。
回答by Apoorva Ambhoj
You can shorten the function and use this in ES6- using arrow function (lambda)
您可以使用箭头函数(lambda)缩短函数并在 ES6 中使用它
let m2= new Map([...m.entries()].sort((a,b) => b[1] - a[1]))