diff --git a/README.md b/README.md index 445219c..5f95344 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ Refer to the documenation for examples on how to use table-sort-js with [HTML](h | "dates-ymd-sort" | Sorts dates in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) yyyy/mm/dd format. e.g (2021/10/28). Use "/" or "-" as separator. | | "file-size-sort" | Sorts file sizes(B->TiB) uses the binary prefix. (e.g 10 B, 100 KiB, 1 MiB); optional space between number and prefix. | | "runtime-sort" | Sorts runtime in hours minutes and seconds e.g (10h 1m 20s). Useful for sorting the GitHub actions Run time column... | +| "numeric-sort" | Sorts numbers - Positive, Negative (Both minus and parenthesis representations), Decimals (can also be used for Semantic Versioning)| | <th> Classes that change defaults. | Description | | ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | diff --git a/public/table-sort.js b/public/table-sort.js index ecd643b..a897471 100644 --- a/public/table-sort.js +++ b/public/table-sort.js @@ -329,32 +329,35 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { const isAlphaSort = th.classList.contains("alpha-sort"); const isNumericSort = th.classList.contains("numeric-sort"); function sortAscending(a, b) { - function handleNumbers(str1, str2) { - let num1, num2; - str1 = str1.slice(0, str1.indexOf("#")); - str2 = str2.slice(0, str2.indexOf("#")); - - if (str1.match(/^\((\d+(?:\.\d+)?)\)$/)) { - num1 = -1 * Number(str1.slice(1, -1)); - } else { - num1 = Number(str1); - } - - if (str2.match(/^\((\d+(?:\.\d+)?)\)$/)) { - num2 = -1 * Number(str2.slice(1, -1)); + function parseNumberFromString(str) { + let num; + str = str.slice(0, str.indexOf("#")); + if (str.match(/^\((\d+(?:\.\d+)?)\)$/)) { + num = -1 * Number(str.slice(1, -1)); } else { - num2 = Number(str2); + num = Number(str); } + return num; + } + + function strLocaleCompare(str1, str2) { + return str1.localeCompare( + str2, + navigator.languages[0] || navigator.language, + { numeric: !isAlphaSort, ignorePunctuation: !isPunctSort } + ); + } + + function handleNumbers(str1, str2) { + let num1, num2; + num1 = parseNumberFromString(str1); + num2 = parseNumberFromString(str2); if (!isNaN(num1) && !isNaN(num2)) { return num1 - num2; } else { - return str1.localeCompare( - str2, - navigator.languages[0] || navigator.language, - { numeric: !isAlphaSort, ignorePunctuation: !isPunctSort } - ); - } + return strLocaleCompare(str1, str2); + } } if (a.includes(`${fillValue}#`)) { @@ -363,12 +366,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { return -1; } else if (isNumericSort) { return handleNumbers(a, b); - } else{ - return a.localeCompare( - b, - navigator.languages[0] || navigator.language, - { numeric: !isAlphaSort, ignorePunctuation: !isPunctSort } - ); + } else { + return strLocaleCompare(a, b); } } diff --git a/test/table.test.js b/test/table.test.js index 1a98880..44a6453 100644 --- a/test/table.test.js +++ b/test/table.test.js @@ -408,47 +408,7 @@ test("Sort decimal numbers", () => { }); }); -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"], - }); -}); - -test("Sort all combination of negative and positive integers and decimal numbers", () => { - expect( - createTestTable( - { - col0: ["1.05", "-2.3", "-3", "1", "-6", "", "14"], - }, - { classTags: "numeric-sort" } - ) - ).toStrictEqual({ - col0: ["-6","-3","-2.3","1","1.05","14",""], - }); -}); - - -test("Sort all negative numbers with parenthesis as well", () => { +test("Sort all combination positive, negative numbers with parenthesis as well", () => { expect( createTestTable( { @@ -470,6 +430,6 @@ test("Sort all combination of negative and positive integers and decimal numbers { classTags: "numeric-sort" } ) ).toStrictEqual({ - col0: ["{1}","-6","-3","-2.3","(0.5)","1","1.05","1a","b","(c)",""], + col0: ["-6","-3","-2.3","(0.5)","1","1.05","{1}","1a","b","(c)",""], }); }); \ No newline at end of file