You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用ng2-order-pipe按对象子属性排序数组?

Sorting by nested properties with ng2-order-pipe

Great to hear you're already using ng2-order-pipe successfully for top-level properties! Sorting by a nested property like customer.Group.Name is actually straightforward with this pipe—you just need to pass the full nested path as the sort key, exactly how you'd access it in your TypeScript code.

Here's how to update your template:

<p *ngFor="let customer of customers | orderBy: 'Group.Name'">{{ customer.Name }}</p>

Bonus: Reverse sorting

If you ever need to sort in descending order, just prepend a - to the nested path:

<p *ngFor="let customer of customers | orderBy: '-Group.Name'">{{ customer.Name }}</p>

A quick heads-up: Make sure that every customer in your array has a Group property (and that Group itself has a Name property) to avoid unexpected behavior. If some entries might be missing these properties, you might want to handle that in your data fetching/processing step first, or use Angular's safe navigation operator (?.) in your template if needed—though the pipe should gracefully handle undefined values by grouping them at the start or end of the sorted list.

内容的提问来源于stack exchange,提问作者Montaser Mohtaseb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:27:20