Skip to content

Commit

Permalink
feat(perf): improve date sorting speed
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Feb 7, 2021
1 parent 16c4984 commit 258da22
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/common/src/sortComparers/dateUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ const moment = moment_['default'] || moment_; // patch to fix rollup "moment has
export function compareDates(value1: any, value2: any, sortDirection: number, sortColumn: Column, gridOptions: GridOption, format: string | moment_.MomentBuiltinFormat, strict?: boolean) {
let diff = 0;
const checkForUndefinedValues = sortColumn?.valueCouldBeUndefined ?? gridOptions?.cellValueCouldBeUndefined ?? false;
const date1 = moment(value1, format, strict);
const date2 = moment(value2, format, strict);

if (value1 === null || value1 === '' || (checkForUndefinedValues && value1 === undefined) || !moment(value1, format, strict).isValid()) {
if (value1 === null || value1 === '' || (checkForUndefinedValues && value1 === undefined) || !date1.isValid()) {
diff = -1;
} else if (value2 === null || value2 === '' || (checkForUndefinedValues && value2 === undefined) || !moment(value2, format, strict).isValid()) {
} else if (value2 === null || value2 === '' || (checkForUndefinedValues && value2 === undefined) || !date2.isValid()) {
diff = 1;
} else {
const date1 = moment(value1, format, strict);
const date2 = moment(value2, format, strict);
diff = parseInt(date1.format('X'), 10) - parseInt(date2.format('X'), 10);
diff = date1.valueOf() < date2.valueOf() ? -1 : 1;
}

return sortDirection * (diff === 0 ? 0 : (diff > 0 ? 1 : -1));
return sortDirection * diff;
}

/** From a FieldType, return the associated Date SortComparer */
Expand Down

0 comments on commit 258da22

Please sign in to comment.