diff options
author | 2019-05-29 18:05:10 +0000 | |
---|---|---|
committer | 2019-05-29 18:05:10 +0000 | |
commit | b86fe0e27bc66ecae386007c1d751f82cbf920cd (patch) | |
tree | 353abe10e7bed944b904d9c6dfd26ae305a1d469 /cds-ui/client/src/app/common/shared/pipes/sort.pipe.ts | |
parent | 7f44056b720529928c165a9fb11e5ec7a32b404f (diff) | |
parent | b0bda9b6310a84ce00a747926c78b04b66eac8ae (diff) |
Merge "CCSDK-1308- Files sorting"
Diffstat (limited to 'cds-ui/client/src/app/common/shared/pipes/sort.pipe.ts')
-rw-r--r-- | cds-ui/client/src/app/common/shared/pipes/sort.pipe.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/cds-ui/client/src/app/common/shared/pipes/sort.pipe.ts b/cds-ui/client/src/app/common/shared/pipes/sort.pipe.ts new file mode 100644 index 000000000..f878a2256 --- /dev/null +++ b/cds-ui/client/src/app/common/shared/pipes/sort.pipe.ts @@ -0,0 +1,54 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2019 IBM Intellectual propertyNameerty. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +============LICENSE_END============================================ +*/ + +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'sort', + pure:false, +}) +export class SortPipe implements PipeTransform { + + transform(arrayData: any[], direcion: string, propertyName?: string): any { + if (!arrayData) { + return []; + } + if (!direcion || !propertyName) { + return arrayData + } + if (arrayData.length > 0) { + const _direction = direcion === 'asc' ? -1 : 1, + isArray = Array.isArray(arrayData), + arrayDataType = typeof arrayData[0], + flag = isArray && arrayDataType === 'object' ? true : isArray && arrayDataType !== 'object' ? false : true; + arrayData.sort((a, b) => { + a = flag ? a[propertyName] : a; + b = flag ? b[propertyName] : b; + if (typeof a === 'string') { + return a.toLowerCase() > b.toLowerCase() ? -1 * _direction : 1 * _direction; + } else if (typeof a === 'number') { + return a - b > 0 ? -1 * _direction : 1 * _direction; + } + }); + } + return arrayData; + } +}
\ No newline at end of file |