我有以下问题。我必须按日期对一系列活动进行排序。问题在于“日期”键并不总是相同。如果是一天的活动,它看起来像:
date: "2019-10-25T00:00:00.000Z"
但是如果是两天或更长时间,它看起来就像:
date:{dateFrom: "2017-05-13T00:00:00.000Z", dateTo: "2017-05-14T00:00:00.000Z"}
我尝试了一种普通的排序或对两个永不为空的键进行排序的函数类型。
那么,如何按日期对该数组进行排序?
activities = [{
"date": {dateTo:"2019-05-20T00:00:00.000Z", dateFrom: "not important"},
activity: 5
},{
"date": {dateTo:"2019-05-05T00:00:00.000Z", dateFrom: "not important"},
activity: 2
},{
"date": "2019-05-10T00:00:00.000Z",
activity: 3
},{
"date": "2019-05-25T00:00:00.000Z",
activity: 6
},{
"date": "2019-05-01T00:00:00.000Z",
activity: 1
},{
"date": "2019-05-15T00:00:00.000Z",
activity: 4
}]
答案 0 :(得分:1)
一种解决方案是定义一个类似于getItemDate()
的辅助函数,并结合常规的Array#sort()
方法来实现您所需要的:
const activities = [{
"date": {dateTo:"2019-05-20T00:00:00.000Z", dateFrom: "not important"},
activity: 5
},{
"date": {dateTo:"2019-05-05T00:00:00.000Z", dateFrom: "not important"},
activity: 2
},{
"date": "2019-05-10T00:00:00.000Z",
activity: 3
},{
"date": "2019-05-25T00:00:00.000Z",
activity: 6
},{
"date": "2019-05-01T00:00:00.000Z",
activity: 1
},{
"date": "2019-05-15T00:00:00.000Z",
activity: 4
}];
/* Define helper function that obtains a date timestamp from activities list item. If
date key is an object, item.date.dateTo is used, otherwise item.date is used */
function getItemDate(item) {
let date = (typeof item.date === 'object') ? item.date.dateTo : item.date;
return Date.parse(date);
}
/* Use helper function to sort items in activities list */
activities.sort((a,b) => getItemDate(a) - getItemDate(b))
console.log(activities)
答案 1 :(得分:0)
您可以使用数组的sort
方法并进行自定义比较。在compare
函数内部,您可以从两个对象中获取日期字符串,并借助Date.parse
将其转换为时间戳。之后,您必须比较这些时间戳并返回排序规则(-1、1或0)。
let activities = [{
"date": {dateTo:"2019-05-20T00:00:00.000Z", dateFrom: "not important"},
activity: 5
},{
"date": {dateTo:"2019-05-05T00:00:00.000Z", dateFrom: "not important"},
activity: 2
},{
"date": "2019-05-10T00:00:00.000Z",
activity: 3
},{
"date": "2019-05-25T00:00:00.000Z",
activity: 6
},{
"date": "2019-05-01T00:00:00.000Z",
activity: 1
},{
"date": "2019-05-15T00:00:00.000Z",
activity: 4
}];
function compare( a, b ) {
let aDateString = a.date.dateTo ? a.date.dateTo : a.date;
let bDateString = b.date.dateTo ? b.date.dateTo : b.date;
let aDate = Date.parse(aDateString);
let bDate = Date.parse(bDateString);
if ( aDate < bDate ){
return -1;
}
if ( aDate > bDate ){
return 1;
}
return 0;
}
activities.sort( compare );
console.log(activities);
答案 2 :(得分:0)
检查date
属性是否具有dateTo
属性,并对其值进行排序。
const activities = [{
"date": {
dateTo: "2019-05-20T00:00:00.000Z",
dateFrom: "not important"
},
activity: 5
}, {
"date": {
dateTo: "2019-05-05T00:00:00.000Z",
dateFrom: "not important"
},
activity: 2
}, {
"date": "2019-05-10T00:00:00.000Z",
activity: 3
}, {
"date": "2019-05-25T00:00:00.000Z",
activity: 6
}, {
"date": "2019-05-01T00:00:00.000Z",
activity: 1
}, {
"date": "2019-05-15T00:00:00.000Z",
activity: 4
}]
activities.sort((a, b) => {
const x = a.date.dateTo || a.date
const y = b.date.dateTo || b.date
if (x > y) return 1
if (x < y) return -1
return 0
})
console.log(activities)
答案 3 :(得分:0)
const newActivities = activities.map((act) => {
const { activity, date } = act;
if(typeof date === 'string'){
return { date: Date.parse(date), activity }
}
return { date: Date.parse(date.dateTo), activity}
})
newActivties.sort((a,b) => a - b)