如何使用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

