【发布时间】:2022-09-27 20:44:54
【问题描述】:
假设您有以下类型的对象数组:
type Obj = {
id: number,
created: Date,
title: string
}
你如何在不被类型系统绊倒的情况下按给定的属性排序?例如:
const numberSorted = objArray.sortBy(\"id\");
const dateSorted = objArray.sortBy(\"created\");
const stringSorted = objArray.sortBy(\"title\");
-
你有没有尝试过?你遇到了哪些问题?
-
@GuillaumeBrunerie 我查看了 Array.prototype.sort() 文档并提出了以下
sort((a,b) => a[property] > b[property] ? 1 : -1)。 IMO,这很冗长,所以我希望能有一些更圆滑的东西。
标签: typescript