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条经验 获得超3个赞
最好的方法是使用 as, 函数,使我们能够灵活地覆盖任何类型的数组数据的数组的排序行为,并且可以用于自定义属性的自定义排序顺序。看看我举的最后一个例子。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)
添加回答
举报