2 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以使用鍵對數(shù)組進(jìn)行排序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貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
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);
添加回答
舉報(bào)