From 270aa525918065aa3585d35ba2ffc5affd1a1668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Fern=C3=A1ndez?= Date: Sat, 28 Sep 2024 02:33:36 +0200 Subject: [PATCH] Fixes issue #441: Dates were not parsed on sorting --- .../search/expressions/order_by_and_limit.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/services/search/expressions/order_by_and_limit.ts b/src/services/search/expressions/order_by_and_limit.ts index 9d0ff31c8..7169e5d02 100644 --- a/src/services/search/expressions/order_by_and_limit.ts +++ b/src/services/search/expressions/order_by_and_limit.ts @@ -68,8 +68,16 @@ class OrderByAndLimitExp extends Expression { return larger; } + + // if both are dates, then parse them for dates comparison + if (typeof valA === "string" && this.isDate(valA) && + typeof valB === "string" && this.isDate(valB)) { + valA = new Date(valA); + valB = new Date(valB); + } + // if both are numbers, then parse them for numerical comparison - if (typeof valA === "string" && this.isNumber(valA) && + else if (typeof valA === "string" && this.isNumber(valA) && typeof valB === "string" && this.isNumber(valB)) { valA = parseFloat(valA); valB = parseFloat(valB); @@ -99,6 +107,10 @@ class OrderByAndLimitExp extends Expression { return noteSet; } + isDate(date: number | string) { + return (new Date(date) !== "Invalid Date") && !isNaN(new Date(date)); + } + isNumber(x: number | string) { if (typeof x === 'number') { return true;