Skip to content

Commit

Permalink
Fix sorting in TableEditorView
Browse files Browse the repository at this point in the history
More robust sorting for edge cases.
Fix issue caused by changing isNaN to Number.isNaN
for eslint fixes.

Issue #1758
  • Loading branch information
robyngit committed Sep 10, 2024
1 parent d3e415a commit fa0e876
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions src/js/views/TableEditorView.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ define([
"markdownTableToJson",
"text!templates/tableEditor.html",
], (_, $, Backbone, markdownTableFromJson, markdownTableToJson, Template) => {
// a utility function to check if a value is empty for sorting
const valIsEmpty = (x) =>
x === "" || x === undefined || x === null || Number.isNaN(x);

/**
* @class TableEditorView
* @classdesc A view of an HTML textarea with markdown editor UI and preview
Expand Down Expand Up @@ -461,9 +465,9 @@ define([

/**
* Compare Functions for sorting - ascending
* @param {number} currentCol The number of the column to sort
* @param {*} a One of two items to compare
* @param {*} b The second of two items to compare
* @param {number} currentCol The number of the column to sort
* @param {*} a One of two items to compare
* @param {*} b The second of two items to compare
* @returns {number} A number indicating the order to place a vs b in the
* list. It it returns less than zero, then a will be placed before b in
* the list.
Expand All @@ -472,48 +476,49 @@ define([
try {
let valA = a[currentCol];
let valB = b[currentCol];
if (valA === "") return 1;
if (valB === "") return -1;

if (valIsEmpty(valA)) return 1;
if (valIsEmpty(valB)) return -1;

// Check for strings and numbers
if (Number.isNaN(valA) || Number.isNaN(valB)) {
valA = valA.toUpperCase();
valB = valB.toUpperCase();
if (valA < valB) return -1;
if (valA > valB) return 1;
return 0;
if (typeof valA === "number" && typeof valB === "number") {
return valA - valB;
}
return valA - valB;
valA = String(valA).toUpperCase();
valB = String(valB).toUpperCase();
if (valA < valB) return -1;
if (valA > valB) return 1;
return 0;
} catch (e) {
return 0;
}
},

/**
* Descending compare function
* @param {number} currentCol The number of the column to sort
* @param {*} a One of two items to compare
* @param {*} b The second of two items to compare
* @returns {number} A number indicating the order to place a vs
* @param {number} currentCol The number of the column to sort
* @param {*} a One of two items to compare
* @param {*} b The second of two items to compare
* @returns {number} A number indicating the order to place a vs
* b in the list. It it returns less than zero, then a will be placed
* before b in the list.
*/
dscSort(currentCol, a, b) {
try {
let valA = a[currentCol];
let valB = b[currentCol];
if (valA === "") return 1;
if (valB === "") return -1;
if (valIsEmpty(valA)) return -1;
if (valIsEmpty(valB)) return 1;

// Check for strings and numbers
if (Number.isNaN(valA) || Number.isNaN(valB)) {
valA = valA.toUpperCase();
valB = valB.toUpperCase();
if (valA < valB) return 1;
if (valA > valB) return -1;
return 0;
if (typeof valA === "number" && typeof valB === "number") {
return valB - valA;
}
return valB - valA;
valA = String(valA).toUpperCase();
valB = String(valB).toUpperCase();
if (valB < valA) return -1;
if (valB > valA) return 1;
return 0;
} catch (e) {
return 0;
}
Expand Down

0 comments on commit fa0e876

Please sign in to comment.