import { Pipe , PipeTransform , NgModule } from '@angular/core' ; import { isArray } from '../utils/utils' ; @ Pipe ( { name : 'orderBy' , } ) export class OrderByPipe implements PipeTransform { private static _orderBy ( a , b ) { if ( a instanceof Date && b instanceof Date ) { return a < b ? - 1 : a > b ? 1 : 0 ; } const floatA = parseFloat ( a ) ; const floatB = parseFloat ( b ) ; if ( typeof a === 'string' && typeof b === 'string' && ( isNaN ( floatA ) || isNaN ( floatB ) ) ) { const lowerA = a . toLowerCase ( ) ; const lowerB = b . toLowerCase ( ) ; return lowerA < lowerB ? - 1 : lowerA > lowerB ? 1 : 0 ; } return floatA < floatB ? - 1 : floatA > floatB ? 1 : 0 ; } transform ( input , config = '+' ) { if ( ! isArray ( input ) ) { return input ; } const configIsArray = isArray ( config ) ; if ( ! configIsArray || ( configIsArray && config . length === 1 ) ) { const propertyToCheck = configIsArray ? config [ 0 ] : config ; const first = propertyToCheck . substr ( 0 , 1 ) ; const desc = first === '-' ; if ( ! propertyToCheck || propertyToCheck === '-' || propertyToCheck === '+' ) { return [ ... input ] . sort ( ( a , b ) => { const comparator = OrderByPipe . _orderBy ( a , b ) ; return desc ? - comparator : comparator ; } ) ; } else { const property = first === '+' || desc ? propertyToCheck . substr ( 1 ) : propertyToCheck ; return [ ... input ] . sort ( ( a , b ) => { const comparator = OrderByPipe . _orderBy ( a [ property ] , b [ property ] ) ; return desc ? - comparator : comparator ; } ) ; } } else { return [ ... input ] . sort ( ( a , b ) => { for ( let i = 0 ; i < config . length ; ++ i ) { const first = config [ i ] . substr ( 0 , 1 ) ; const desc = first === '-' ; const property = first === '+' || desc ? config [ i ] . substr ( 1 ) : config [ i ] ; const comparator = OrderByPipe . _orderBy ( a [ property ] , b [ property ] ) ; const comparison = desc ? - comparator : comparator ; if ( comparison !== 0 ) { return comparison ; } } return 0 ; } ) ; } } } @ NgModule ( { declarations : [ OrderByPipe ] , exports : [ OrderByPipe ] , } ) export class NgOrderByPipeModule { }