Custom sorting pipe for Angular 6

Ankit Prajapati

I am trying to sort my data in the ngFor loop , but my custom pipe doesnot work as intened , can anybody help me out...

  export class OrderbyLastMessagePipe implements PipeTransform {

  transform(array: Array<string>, args?: any): Array<string> {

    if (array !== undefined && array !== null && args !== '') {

      console.log('args', array);

        const direction = args === 'desc' ? 1 : -1;
        array.sort((a: any, b: any) => {

          console.log('args', a);

          if (a['lastUpdatedat'] < b['lastUpdatedat']) {
                return -1 * direction;
            } else if (a['lastUpdatedat'] > b['lastUpdatedat']) {
                return 1 * direction;
            } else {
                return 0;
            }
        });
    }
    return array;
}

My Component ng container is this , i am using two filters one for search and another for sort on specific lastupdatedat value which is the timestamp ...

 <ng-container *ngFor="let User of uLists | orderbyLastMessage:'uLists':true">         
freepowder

very similar answered here

Angular Custom Order Pipe sorting array correctly

The user search must be done with input type text, keyup event, and array.filter method.

Good luck!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related