I am trying to sort a array object by date
this.result.sort((a, b) => {
if (this.filter.orderBy === 'possession') {
//if(moment(a.overview.possession_starts).isAfter(moment(b.overview.possession_starts)))
//sort string ascending
if (moment(a.overview.possession_starts) > moment(b.overview.possession_starts))
//sort string ascending
return 1;
return 0;
});
is working fine in Mozilla and chrome
Your callback returns 1
, 0
or undefined
. This isn't what's required by the sort function . Fix it:
if (this.filter.orderBy === 'possession') {
this.result.sort((a, b) => {
return moment(a.overview.possession_starts).milliseconds() - moment(b.overview.possession_starts).milliseconds();
});
}
您可以用这一行替换您的代码。
this.filter.orderBy === 'possession' && this.result.sort((a, b) => moment(a.overview.possession_starts).milliseconds() - moment(b.overview.possession_starts).milliseconds());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.