我正在表格中展示一组类型为“请求”的数组。我想对表格的列进行排序,所以计划为每个列标题创建一个点击方法。这些方法会根据该列中显示的属性值对数组进行排序。
public sortProduct(): void {
this.requests.sort((a, b) => {
if (a.productName < b.productName)
return -1;
if (a.productName > b.productName)
return 1;
return 0;
});
if (!this.productSortOrder) {
this.requests.reverse();
this.productSortOrder = true;
} else {
this.productSortOrder = false;
}
}
这个方法是有效的,但现在我需要为每一列创建一个方法。我正在寻找一种可以像这样调用排序方法的方式:
this.requests.sortMethod(property, order);
此方法将基于数组中对象的属性和给定的排序顺序对请求数组进行排序。 我该怎么做?我猜我正在寻找类似于C#中的Func<>的东西。
Func
相同,上面的签名将与Func<Product, T>
相同(我假设您具有 C# 背景,并且这对您有意义)。语法为(paramList) => returnType
,其中paramList
是param1: pramType1, param2: pramType2,...
。 - Titian Cernicova-Dragomir