[英]How to sort the array of objects based on nested property in Aurelia/Typescript
我必须按其嵌套属性对对象数组进行排序。 更准确地说,我必须根据payment.paymentDate属性对从service.payments对象派生的付款进行排序。
有没有办法自定义排序转换器代码或修改 html,以便我可以根据支付日期对付款进行排序?
有关更多详细信息,请参阅下面的代码:
这是 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();
}
}
这是数据:
根据您在下面提供的排序 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.