Files
iTunes/dashboard/src/app/pipes/sort-by.pipe.ts
Maxence G. de Montauzan 6300e9cfc7 Refactoring
Move Top Played part.
Move pipes and dashboard
Rename object folder to model
Doc pipes
2018-03-20 23:32:21 +01:00

41 lines
1.1 KiB
TypeScript

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'sortBy'
})
export class SortByPipe implements PipeTransform {
/**
* Sort an array by multiple elements.
*
* Example to sort an array respectively by Year, Album, Disc Number, etc.
* Typescript usage:
* <pre>
* new SortByPipe().transform(this.songs, 'Year', 'Album', 'Disc Number', 'Track Number', 'Play Count');
* </pre>
* In HTML usage:
* <pre>
* <tr *ngFor="let song of songs | sortBy : 'Year':'Album':'Disc Number':'Track Number'">
* </pre>
*
* @param array array to sort
* @param args elements used to sort in oder
*/
transform(array: Array<any>, ...args: any[]): Array<any> {
array.sort((a: any, b: any) => {
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (a[arg] === undefined && b[arg] !== undefined) { return -1; }
if (a[arg] !== undefined && b[arg] === undefined) { return 1; }
if (a[arg] < b[arg]) { return -1; }
if (a[arg] > b[arg]) { return 1; }
}
return 0;
});
return array;
}
}