I need to apply alphanumeric sorting on grid but not able to get sorting event from grid.
Asked
Active
Viewed 367 times
1 Answers
1
There are three event emitters that can be used, topic on the subject.
sorting - emitted before sorting expressions are applied
sortingDone - Emitted after sorting is completed
<igx-grid #grid [data]="localData" [autoGenerate]="true" (sortingDone)="sortingDone($event)">
sortingExpressionsChange - Emitted before sorting is performed
Sorting is performed using the DefaultSortingStrategy
algorithm. Any IgxColumnCOmponent
or ISortingExpression
can use a custom implementation of the ISortingStrategy
.
You can define your own sorting strategy by extending the default one or by implementing ISortingStrategy
:
- Example with
CustomSortingStrategy
export class CustomSortingStrategy extends DefaultSortingStrategy {
...
}
...
const strategy = CustomSortingStrategy.instance();
grid.sort({fieldName: 'Name', dir: SortingDirection.Asc, ignoreCase: true, strategy: CustomSortingStrategy.instance()});
- Example with
ISortingStrategy
implementation:
export class SortByParityComponent implements ISortingStrategy {
public sort(data: any[], fieldName: string, dir: SortingDirection) {
const key = fieldName;
const reverse = (dir === SortingDirection.Desc ? -1 : 1);
const cmpFunc = (obj1, obj2) => this.compareObjects(obj1, obj2, key, reverse);
return data.sort(cmpFunc);
}
protected sortByParity(a: any, b: any) {
return a % 2 === 0 ? -1 : b % 2 === 0 ? 1 : 0;
}
protected compareObjects(obj1, obj2, key: string, reverse: number) {
const a = obj1[key];
const b = obj2[key];
return reverse * this.sortByParity(a, b);
}
}
...
grid.sort({
fieldName: 'ID',
dir: SortingDirection.Desc,
ignoreCase: false,
strategy: new SortByParityComponent()
});
Here you can find a working sample with CustomSortingStrategy.

Zdravko Kolev
- 1,569
- 14
- 20