projects/i-components/src/lib/components/data-grid/CustomSortingStrategy.ts
Custom Sorting strategy to be used by template columns
ISortingStrategy
Methods |
|
Protected compareObjects | ||||||||||||||||||
compareObjects(obj1: any, obj2: any, fieldName: string, dir: SortingDirection, ignoreCase: boolean)
|
||||||||||||||||||
Compare record objects
Parameters :
Returns :
number
|
Private getPropertyValue |
getPropertyValue(obj: any, propertyName: string)
|
Get the value corresponding to a specific property of an object
Returns :
any
|
Public sort | |||||||||||||||
sort(data: any[], fieldName: string, dir: SortingDirection, ignoreCase: boolean)
|
|||||||||||||||
Overrides sorting method for custom template columns
Parameters :
Returns :
any
|
Protected sortByParity |
sortByParity(a: any, b: any, dir: SortingDirection)
|
Custom sorting function Allows custom template columns to be sorted in both directions
Returns :
0 | 1 | -1
|
Protected sortComparableObjects | ||||||||||||
sortComparableObjects(a: IComparable<any>, b: IComparable<any>, dir: SortingDirection)
|
||||||||||||
Custom sorting function for objects that implements IComparable interface
Parameters :
Returns :
number
|
import { IComparable } from '@mobilize/wms-framework';
import { SortingDirection, ISortingStrategy } from 'igniteui-angular';
/**
* Custom Sorting strategy to be used by template columns
*
* @export
* @class CustomSortingStrategy
* @implements {ISortingStrategy}
*/
export class CustomSortingStrategy implements ISortingStrategy {
/**
* Overrides sorting method
* for custom template columns
*
* @param {any[]} data
* @param {string} fieldName
* @param {SortingDirection} dir
* @param {boolean} ignoreCase
* @return {*}
* @memberof CustomSortingStrategy
*/
public sort(
data: any[],
fieldName: string,
dir: SortingDirection,
ignoreCase: boolean
) {
const cmpFunc = (a, b) =>
this.compareObjects(a, b, fieldName, dir, ignoreCase);
return data.sort(cmpFunc);
}
/**
* Compare record objects
*
* @protected
* @param {*} obj1
* @param {*} obj2
* @param {string} fieldName
* @param {SortingDirection} dir
* @param {boolean} ignoreCase
* @return {*}
* @memberof CustomSortingStrategy
*/
protected compareObjects(
obj1: any,
obj2: any,
fieldName: string,
dir: SortingDirection,
ignoreCase: boolean
) {
let a = this.getPropertyValue(obj1, fieldName);
let b = this.getPropertyValue(obj2, fieldName);
if (
typeof a?.CompareTo === 'function' &&
typeof b?.CompareTo === 'function'
) {
return this.sortComparableObjects(a, b, dir);
}
if (ignoreCase) {
a = typeof a?.toLowerCase === 'function' ? a.toLowerCase() : a;
b = typeof b?.toLowerCase === 'function' ? b.toLowerCase() : b;
}
return this.sortByParity(a, b, dir);
}
/**
* Custom sorting function
* Allows custom template columns to be sorted in
* both directions
*
* @protected
* @param {*} a
* @param {*} b
* @param {SortingDirection} dir
* @return {*}
* @memberof CustomSortingStrategy
*/
protected sortByParity(a: any, b: any, dir: SortingDirection) {
if (dir === SortingDirection.Asc) {
if (a > b || !b) {
return +1;
}
if (a < b || !a) {
return -1;
}
return 0;
} else if (dir === SortingDirection.Desc) {
if (a < b || !a) {
return +1;
}
if (a > b || !b) {
return -1;
}
return 0;
} else {
return 0;
}
}
/**
* Custom sorting function for objects that implements IComparable interface
*
* @protected
* @param {IComparable<any>} a
* @param {IComparable<any>} b
* @param {SortingDirection} dir
* @return {number}
* @memberof CustomSortingStrategy
*/
protected sortComparableObjects(
a: IComparable<any>,
b: IComparable<any>,
dir: SortingDirection
): number {
if (dir === SortingDirection.Asc) {
if (a && a.CompareTo(b) > 0) {
return +1;
}
if (b && b.CompareTo(a) > 0) {
return -1;
}
return 0;
} else if (dir === SortingDirection.Desc) {
if (a && a.CompareTo(b) < 0) {
return +1;
}
if (b && b.CompareTo(a) < 0) {
return -1;
}
return 0;
} else {
return 0;
}
}
/**
* Get the value corresponding to a specific property of an object
*
* @param {*} obj
* @param {string} propertyName
* @returns {*}
* @memberof CustomSortingStrategy
*/
private getPropertyValue(obj: any, propertyName: string): any {
const properties = propertyName.split('.');
let value = obj;
properties.forEach((prop: string) => {
value = value[prop];
});
return value;
}
}