Skip to content

Commit

Permalink
fix: handling negative and decimal numeric cases #63 and #64
Browse files Browse the repository at this point in the history
  • Loading branch information
scodes73 committed May 16, 2023
1 parent 58397c9 commit cc15c14
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
7 changes: 6 additions & 1 deletion public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
// Doesn't infer dates with delimiter "."; as could capture semantic version numbers.
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
const numericRegex = /^[+-]?(?:\d*[.,])?\d+$/;
const inferableClasses = {
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
dmyDates: { regexp: dmyRegex, class: "dates-dmy-sort", count: 0 },
ymdDates: { regexp: ymdRegex, class: "dates-ymd-sort", count: 0 },
numericRegex: {regexp: numericRegex, class: "numeric-sort",count:0}
};
let classNameAdded = false;
let regexNotFoundCount = 0;
Expand Down Expand Up @@ -325,12 +327,15 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {

const isPunctSort = th.classList.contains("punct-sort");
const isAlphaSort = th.classList.contains("alpha-sort");
const isNumericSort = th.classList.contains("numeric-sort");
function sortAscending(a, b) {
if (a.includes(`${fillValue}#`)) {
return 1;
} else if (b.includes(`${fillValue}#`)) {
return -1;
} else {
} else if (isNumericSort) {
return Number(a.substring(0, a.indexOf("#"))) - Number(b.substring(0, b.indexOf("#")))
} else{
return a.localeCompare(
b,
navigator.languages[0] || navigator.language,
Expand Down
39 changes: 39 additions & 0 deletions test/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,42 @@ test("dates-ymd-sort: ISO 8601 style yyyy/mm/dd; delim . or / or -", () => {
col0: ["2023-03-9", "2023/4/6", "2023/4/32", "2023/09/6", "2023.12.16"],
});
});

test("Sort decimal numbers", () => {
expect(
createTestTable(
{
col0: ["0.1", "0.2", "0.3", "0.11", "0.13", "0.13", "0.14"],
},
{ classTags: "numeric-sort" }
)
).toStrictEqual({
col0: ["0.1", "0.11", "0.13", "0.13", "0.14", "0.2", "0.3"],
});
});

test("Sort combination of negative and positive numbers", () => {
expect(
createTestTable(
{
col0: ["1", "-2", "-3", "4", "-6", "5", "14"],
},
{ classTags: "numeric-sort" }
)
).toStrictEqual({
col0: ["-6","-3","-2","1","4","5","14"],
});
});

test("Sort all combination of negative and positive integers and decimal numbers", () => {
expect(
createTestTable(
{
col0: ["1.05", "-2.3", "-3", "1", "-6", "5", "14"],
},
{ classTags: "numeric-sort" }
)
).toStrictEqual({
col0: ["-6","-3","-2.3","1","1.05","5","14"],
});
});

0 comments on commit cc15c14

Please sign in to comment.