4 回答

TA貢獻1789條經驗 獲得超10個贊
您可以使用傳遞給 的自定義排序函數基于兩個參數進行排序Array.prototype.sort
const data = [
{
stateCode: 'CH',
startDate: new Date('06/12/2020 08:00'),
startDateWithDelay: new Date('06/12/2020 08:00'),
},
{
stateCode: 'LA',
startDate: new Date('06/12/2020 08:00'),
startDateWithDelay: new Date('06/12/2020 08:30'),
},
{
stateCode: 'NY',
startDate: new Date('06/12/2020 06:00'),
startDateWithDelay: new Date('06/12/2020 06:00')
}
]
const sorted = data.sort((a, b) => {
const startDateComparison = a.startDate - b.startDate
if (startDateComparison !== 0) return startDateComparison
return a.startDateWithDelay - b.startDateWithDelay
})
console.log(sorted)

TA貢獻1872條經驗 獲得超4個贊
最好的方法是使用 as, 函數,使我們能夠靈活地覆蓋任何類型的數組數據的數組的排序行為,并且可以用于自定義屬性的自定義排序順序??纯次遗e的最后一個例子。Array.prototype.sortArray.prototype.sort
const data = [
{
stateCode: "CH",
startDate: new Date("06/12/2020 08:00"),
startDateWithDelay: new Date("06/12/2020 08:00"),
},
{
stateCode: "LA",
startDate: new Date("06/12/2020 08:00"),
startDateWithDelay: new Date("06/12/2020 08:30"),
},
{
stateCode: "NY",
startDate: new Date("06/12/2020 06:00"),
startDateWithDelay: new Date("06/12/2020 06:00"),
},
];
const sorted = data.sort((a, b) => {
// one liner using conditional operator
return a.startDate - b.startDate === 0
? a.startDateWithDelay - b.startDateWithDelay
: a.startDate - b.startDate;
});
console.log(sorted);
在同一示例中,如果我們必須按“LA”,“NY”和“CH”的順序對狀態進行排序,那么我們也可以這樣做,如下面的示例所示。
const data = [
{
stateCode: "CH",
startDate: new Date("06/12/2020 08:00"),
startDateWithDelay: new Date("06/12/2020 08:00"),
},
{
stateCode: "LA",
startDate: new Date("06/12/2020 08:00"),
startDateWithDelay: new Date("06/12/2020 08:30"),
},
{
stateCode: "NY",
startDate: new Date("06/12/2020 06:00"),
startDateWithDelay: new Date("06/12/2020 06:00"),
},
];
// Custom Sorting based on States in order of 'LA', 'NY' & 'CH'
const sorted = data.sort((a, b) => {
const sCa = a.stateCode; //state code of a
const sCb = b.stateCode; //state code of b
return (sCa === 'LA' && sCb !== 'LA') || (sCa === 'NY' && sCb === 'CH') ? -1 :
(sCb === 'LA' && sCa !== 'LA') || (sCb === 'NY' && sCa === 'CH') ? 1 : 0;
});
console.log(sorted);

TA貢獻1830條經驗 獲得超3個贊
data = [
{
stateCode: 'CH',
startDate: new Date('06/12/2020 08:00'),
startDateWithDelay: new Date('06/12/2020 08:00'),
},
{
stateCode: 'LA',
startDate: new Date('06/12/2020 08:00'),
startDateWithDelay: new Date('06/12/2020 08:30'),
},
{
stateCode: 'NY',
startDate: new Date('06/12/2020 06:00'),
startDateWithDelay: new Date('06/12/2020 06:00')
}
]
我們可以使用數組的內置排序函數,因此我們將比較 startDate 如果它們相同,我們將在 startDate 上排序,否則繼續在 startDate 上排序。
let data2 = data.sort((s1, s2) => s1.startDate == s2.startDate ?
s1.startDateWithDelay - s2.startDateWithDelay :
s1.startDate - s2.startDate);
排序完成后,我們可以使用map來修改結果,并加入預期輸出的排序狀態列表。
let states = data2.map(s => s.stateCode).join(', ');
這將輸出預期的結果。
NY, CH, LA

TA貢獻1802條經驗 獲得超5個贊
你可以做這樣的事情
data = [
{
stateCode: 'CH',
startDate: new Date('06/12/2020 08:00'),
startDateWithDelay: new Date('06/12/2020 08:00'),
},
{
stateCode: 'LA',
startDate: new Date('06/12/2020 08:00'),
startDateWithDelay: new Date('06/12/2020 08:30'),
},
{
stateCode: 'NY',
startDate: new Date('06/12/2020 06:00'),
startDateWithDelay: new Date('06/12/2020 06:00')
}
];
data.sort((a,b)=>{
if(a.startDate.getTime() < b.startDate.getTime()){
return -1
}else if(a.startDate.getTime() < b.startDate.getTime()){
return 1
}else{
return a.startDateWithDelay.getTime() - b.startDateWithDelay.getTime()
}
});
console.log(data)
添加回答
舉報