Skip to content

Instantly share code, notes, and snippets.

@tomtobac
Last active February 19, 2017 19:07
Show Gist options
  • Save tomtobac/667562a0db83c31e81c07894d9140e58 to your computer and use it in GitHub Desktop.
Save tomtobac/667562a0db83c31e81c07894d9140e58 to your computer and use it in GitHub Desktop.
import { Company } from './../interfaces/company';
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'sortingCompanies'
})
export class SortingCompaniesPipe implements PipeTransform {
transform(companies: Company[], path: string[], order: number): Company[] {
// Check if is not null
if (!companies || !path || !order) return companies;
return companies.sort((a: Company, b: Company) => {
// We go for each property followed by path
path.forEach(property => {
a = a[property];
b = b[property];
})
// Order * (-1): We change our order
return a > b ? order : order * (- 1);
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment