Skip to content

Commit

Permalink
feat(sorts): option to ignore accent while sorting text
Browse files Browse the repository at this point in the history
  • Loading branch information
mcallegario committed Sep 13, 2021
1 parent 13dfd69 commit 1b4fe81
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/common/src/interfaces/gridOption.interface.ts
Expand Up @@ -461,6 +461,12 @@ export interface GridOption {
*/
ignoreAccentOnStringFilter?: boolean;

/**
* Defaults to false, should we ignore any accent while sorting text?
* For example if our text is "José" and we type "Jose" then it won't return unless we use this flag because "é" is not equal to "e"
*/
ignoreAccentOnStringSort?: boolean;

/**
* When using custom Locales (that is when user is NOT using a Translate Service, this property does nothing when used with Translate Service),
* This is useful so that every component of the lib knows the locale.
Expand Down
13 changes: 10 additions & 3 deletions packages/common/src/sortComparers/stringSortComparer.ts
@@ -1,5 +1,6 @@
import { Column, GridOption, SortComparer } from '../interfaces/index';
import { SortDirectionNumber } from '../enums/sortDirectionNumber.enum';
import { removeAccentFromText } from '../services/utilities';

export const stringSortComparer: SortComparer = (value1: any, value2: any, sortDirection: number | SortDirectionNumber, sortColumn?: Column, gridOptions?: GridOption) => {
if (sortDirection === undefined || sortDirection === null) {
Expand All @@ -14,10 +15,16 @@ export const stringSortComparer: SortComparer = (value1: any, value2: any, sortD
position = -1;
} else if (value2 === null || (checkForUndefinedValues && value2 === undefined)) {
position = 1;
} else if (sortDirection) {
position = value1 < value2 ? -1 : 1;
} else {
position = value1 < value2 ? 1 : -1;
if (gridOptions?.ignoreAccentOnStringSort){
value1 = removeAccentFromText(value1, false);
value2 = removeAccentFromText(value2, false);
}
if (sortDirection) {
position = value1 < value2 ? -1 : 1;
} else {
position = value1 < value2 ? 1 : -1;
}
}
return sortDirection * position;
};

0 comments on commit 1b4fe81

Please sign in to comment.