From 258da2238bba3693eada058f9405012f68af150b Mon Sep 17 00:00:00 2001 From: ghiscoding Date: Sat, 6 Feb 2021 23:52:17 -0500 Subject: [PATCH] feat(perf): improve date sorting speed --- packages/common/src/sortComparers/dateUtilities.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/common/src/sortComparers/dateUtilities.ts b/packages/common/src/sortComparers/dateUtilities.ts index b0c6e2cb1..4b4b574e5 100644 --- a/packages/common/src/sortComparers/dateUtilities.ts +++ b/packages/common/src/sortComparers/dateUtilities.ts @@ -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 */