2 回答
TA贡献1784条经验 获得超7个赞
您可以使用键对数组进行排序String#localeCompare
。
let map = new Map([['2020-09-29', { foo: 1, bar: 2 }], ['2020-09-01', { foo: 3, bar: 4 }], ['2020-09-08', { foo: 5, bar: 6 }]]),
sorted = new Map(Array.from(map).sort(([a], [b]) => a.localeCompare(b)));
console.log([...map]);
console.log([...sorted]);
.as-console-wrapper { max-height: 100% !important; top: 0; }
TA贡献1859条经验 获得超6个赞
var input = [{
key: '2020-09-29',
value: {
foo: 1,
bar: 2
}
},
{
key: '2020-09-01',
value: {
foo: 3,
bar: 4
}
},
{
key: '2020-09-08',
value: {
foo: 5,
bar: 6
}
}
]
input.sort((a, b) => {
const first = a.key
const second = b.key
return first > second ? 1 : (first < second ? -1 : 0)
});
console.log(input);
添加回答
举报