【发布时间】:2018-06-21 13:02:41
【问题描述】:
根据这个问题-> Ascending and Descending Sort in Angular 4
我做了同样的管道,但它不能自然地对数字进行排序。我的意思是 2 > 然后 11。
如何修改此管道以对字符串和数字进行排序?
@Pipe({
name: 'orderBy'
})
export class OrderByPipe implements PipeTransform {
transform(records: Array<any>, args?: any): any {
if (records && records.length > 0) {
return records.sort(function (a, b) {
if (a[args.property] < b[args.property]) {
return -1 * args.direction;
} else if (a[args.property] > b[args.property]) {
return 1 * args.direction;
} else {
return 0;
}
});
}
}
}
【问题讨论】:
-
你能举个例子,你的输入和参数是什么样的,你期望的输出是什么?
-
嗨,实际上它与来自链接的完全相同。输入是来自 html 表格列的值。