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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -