javascript - 将 underscore 的 sortBy 转换为 javascript
我有一个数组,它是使用 Underscore 的数组函数 _.sortBy() 排序的,我需要将它转换为使用原生 JS 函数 .sort()。
我的问题是,当我将其转换为普通的原生 array.sort() 函数时,我的 IDE(Webstorm)抛出了如下错误:
TS2362:算术运算的左侧必须是类型 ‘any’、‘number’、‘bigint’ 或枚举类型。
下划线版本
this.interviewDetails.data = _.sortBy(this.interviewDetails.data, function (o) {
return new Date(o.timeslot);
});
原味版本
this.interviewDetails.data.sort((a: any, b: any) => new Date(a.timeslot) - new Date(b.timeslot));
有人能帮我看看这里可能出了什么问题吗?
附注- 请参阅上图中 new Date(a.timeslot) 的红色下划线
答案#1
试试这个:
this.interviewDetails.data.sort((a: any, b: any) => new Date(a.timeslot).getTime() - new Date(b.timeslot).getTime());
