File

src/app/shared/pipes/orderby.pipe.ts

Metadata

name orderby

Methods

transform
transform(array: Array, sort: string, order?: string)
Parameters :
Name Type Optional Description
array Array<any>
sort string
order string true
Returns : any
import { Pipe, PipeTransform } from '@angular/core';
import { OrderParams } from '../components/shared.interface';

@Pipe({
  name: 'orderby'
})
export class OrderByPipe implements PipeTransform {
  transform(array: Array<any>, sort: string, order ?: string) {
    if (!array) {
      return;
    }
    if (!sort) {
      return array;
    }
    if (!order) {
      order = OrderParams.ASC;
    }
    return Object.assign([], array).sort((a: any, b: any) => {
      if (order === OrderParams.ASC) {
        return a[sort] > b[sort] ? 1 : -1;
      } else {
        return a[sort] <= b[sort] ? 1 : -1;
      }
    });
  }
}

results matching ""

    No results matching ""