import { Pipe , PipeTransform } from '@angular/core' ; @ Pipe ( { name : 'sort' , pure : false } ) export class SortPipe implements PipeTransform { static _orderByComparator ( a , b ) { if ( parseFloat ( a ) < parseFloat ( b ) ) return - 1 ; if ( parseFloat ( a ) > parseFloat ( b ) ) return 1 ; return 0 ; } transform ( input , [ config = '+' ] ) { if ( ! Array . isArray ( input ) ) return input ; if ( ! Array . isArray ( config ) || ( Array . isArray ( config ) && config . length == 1 ) ) { var propertyToCheck = ! Array . isArray ( config ) ? config : config [ 0 ] ; var desc = propertyToCheck . substr ( 0 , 1 ) == '-' ; if ( ! propertyToCheck || propertyToCheck == '-' || propertyToCheck == '+' ) { return ! desc ? input . sort ( ) : input . sort ( ) . reverse ( ) ; } else { var property = propertyToCheck . substr ( 0 , 1 ) == '+' || propertyToCheck . substr ( 0 , 1 ) == '-' ? propertyToCheck . substr ( 1 ) : propertyToCheck ; return input . sort ( function ( a , b ) { return ! desc ? SortPipe . _orderByComparator ( a [ property ] , b [ property ] ) : - SortPipe . _orderByComparator ( a [ property ] , b [ property ] ) ; } ) ; } } else { return input . sort ( function ( a , b ) { for ( var i = 0 ; i < config . length ; i ++ ) { var desc = config [ i ] . substr ( 0 , 1 ) == '-' ; var property = config [ i ] . substr ( 0 , 1 ) == '+' || config [ i ] . substr ( 0 , 1 ) == '-' ? config [ i ] . substr ( 1 ) : config [ i ] ; var comparison = ! desc ? SortPipe . _orderByComparator ( a [ property ] , b [ property ] ) : - SortPipe . _orderByComparator ( a [ property ] , b [ property ] ) ; if ( comparison != 0 ) return comparison ; } return 0 ; } ) ; } } }