Skip to content

Commit

Permalink
fix sorting for dates in demo tables
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanPresmytsky committed Oct 17, 2024
1 parent d214246 commit 35fb277
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions server/helpers/getFilterPredicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ exports.simplifyPredicates = simplifyPredicates;
function isDate(val) {
return dayjs_1["default"](val).isValid();
}

exports.isDate = isDate;

var truePredicate = function () { return true; };
function getFilterPredicate(filter) {
filter = simplifyPredicates(filter);
Expand Down
7 changes: 6 additions & 1 deletion server/helpers/getOrderComparer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
exports.__esModule = true;
const { isDate } = require('./getFilterPredicate');

const eqPredicate = function () { return 0; };
// Previous versions use this comparer. While it works great for human-readable strings, it's broken for dates. Also, we need a plain sort in case of 'order' fields.
// const compareScalars = (new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'})).compare;
Expand All @@ -22,7 +24,10 @@ function getOrderComparer(sorting) {
const comparer = function (a, b) {
for (let n = 0; n < sorting.length; n++) {
const fieldName = sorting[n].field;
const compareResult = compareScalars(a[fieldName], b[fieldName], sortingOrders[n]);
const isDateField = isDate(a[fieldName]) && isDate(b[fieldName]);
const compareResult = isDateField
? compareScalars(new Date(a[fieldName]), new Date(b[fieldName]), sortingOrders[n])
: compareScalars(a[fieldName], b[fieldName], sortingOrders[n]);
if (compareResult !== 0) {
return compareResult;
}
Expand Down

0 comments on commit 35fb277

Please sign in to comment.