Orderby pipe in angular 2+

Code: 

import {Pipe, PipeTransform} from '@angular/core';
import * as _ from 'lodash';
import {Many} from 'lodash';

@Pipe({
  name: 'orderBy',
  pure: false
})
export class OrderByPipe implements PipeTransform {

  transform(items: any[], args?: string[], orders?: Many<boolean|'asc'|'desc'>): any {
    if (args && args.length > 0) {
      if (orders) {
        return _.orderBy(items, args, orders);
      } else {
        return _.orderBy(items, args, 'asc');
      }
    } else {
      return items;
    }
  }
}

How to use:

{{ someList | orderBy: ['createdOn']: 'desc' }}

Comments

Popular posts from this blog

MATLAB code for Circular Convolution using Matrix method

Positive number pipe in angular 2+