angularjs - Angular2 - How is best to do sorting of a pipe list? -
i'm using typescript , angular2.
i have pipe filters list of results.
now want sort list in alphabetical etc.
how this?
thanks
you implement custom pipe leverages sort
method of arrays:
import { pipe } "angular2/core"; @pipe({ name: "sort" }) export class arraysortpipe { transform(array: array<string>, args: string): array<string> { array.sort((a: any, b: any) => { if (a < b) { return -1; } else if (a > b) { return 1; } else { return 0; } }); return array; } }
and use pipe leveraging pipe chaining:
<li *ngfor="list | filter | sort"> (...) </li>
it's simple sample arrays string values can have advanced sorting processing (based on object attributes in case of object array, based on sorting parameters, ...).
here plunkr this: https://plnkr.co/edit/wbzqddoqn1oahvqmkqrq?p=preview.
hope helps you, thierry
Comments
Post a Comment