我必须按其嵌套属性对对象数组进行排序。更准确地说,我必须基于 payment.paymentDate 属性对从 service.payments 对象派生的付款进行排序。
是否可以自定义排序转换器代码或修改html,以便我可以基于paymentDate对付款进行排序?
有关更多详细信息,请参见下面的代码:
这是HTML代码:
<ul>
<li repeat.for="payment of service.payments | sort:payment.paymentDate:'ascending'">
${'dashboard:contracts.desc.custPayment' & t: {
value: payment.value,
currency: payment.currency,
date: formatDate(payment.paymentDate)
}}
</li>
</ul>
这是我的SortValueConverter代码:
export class SortValueConverter {
toView(array: {}[], property: string, direction: string) {
let sorted = [].slice.call(array).sort((a, b) => {
if (a[property] > b[property]) return 1;
if (a[property] < b[property]) return -1;
return 0;
});
return direction === 'ascending' ? sorted : sorted.reverse();
}
}
以下是数据:
答案 0 :(得分:1)
基于下面提供的sort valueConverter,您的HTML应该如下所示:
<li repeat.for="payment of service.payments | sort:'paymentDate':'ascending'">
${'dashboard:contracts.desc.custPayment' & t: {
value: payment.value,
currency: payment.currency,
date: formatDate(payment.paymentDate)
}}
</li>
aslo:为什么不仅仅定期使用数组排序功能?
export class SortValueConverter {
toView(arr: any[], property: string, direction: string) {
const sorted = arr.sort((a, b) => a[property] - b[property]));
return direction === 'ascending' ? sorted : sorted.reverse();
}
}