Last active
February 19, 2017 19:07
-
-
Save tomtobac/667562a0db83c31e81c07894d9140e58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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