这里我们用ts定义了一个类:
interface ArchivedArticle{
id: number;
title: string;
authorId: number;
uploadTime: Date; // 如果后端返回的是 Timestamp 类型,这里可以用 Date 类型来表示
archivedTime: Date;
}
响应式对象数组:
const archivedArticle = ref<ArchivedArticle[]>([]);
我们比较时如下格式
archivedArticle.value.sort((a , b) => {return 表达式}
返回大于0的数字表示a>b,反之则反之。
对于数字类型我们直接比较
return a.id - b.id
对于字符串类型:
return a.title.localeCompare(b.title)
对于Date类型:
return a.uploadTime.getTime() - b.uploadTime.getTime()
但是如果你后端传输的是TimeStamp类型,而前端使用Date接收,那么不能直接调用getTime()函数,会报错该函数不存在,
所以我们需要这样:
return new Date(a.uploadTime).getTime() - new Date(b.uploadTime).getTime()