Skip to content

Commit 3396d40

Browse files
numeric-sort handle number with commas… (#116)
* Handle numbers with commas when sorting with numeric sort; still need to do inference. * Change regex to capture numbers with commas.
1 parent 9463e25 commit 3396d40

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

public/index.html

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ <h1>Manual testing of table sort js</h1>
1919
<th class="onload-sort">File Size</th>
2020
<th class="data-sort">data-sort days</th>
2121
<th>dates in dd/mm/yyyy</th>
22+
<th>file version</th>
2223
</tr>
2324
</thead>
2425
<tr class="table-row-1">
@@ -31,6 +32,7 @@ <h1>Manual testing of table sort js</h1>
3132
<td>10b</td>
3233
<td data-sort="2">Tuesday</td>
3334
<td>17/6/1978</td>
35+
<td>1.18.1</td>
3436
</tr>
3537
<tr class="table-row-2">
3638
<td>da Vinci</td>
@@ -42,6 +44,7 @@ <h1>Manual testing of table sort js</h1>
4244
<td>192038998987021b</td>
4345
<td data-sort="3">Wednesday</td>
4446
<td>18/10/2027</td>
47+
<td>239.123.23</td>
4548
</tr>
4649
<tr>
4750
<td>Statham</td>
@@ -53,6 +56,7 @@ <h1>Manual testing of table sort js</h1>
5356
<td>134809b</td>
5457
<td data-sort="5">Friday</td>
5558
<td>4/9/2008</td>
59+
<td>3423.342.34</td>
5660
</tr>
5761
<tr>
5862
<td>Micheal</td>
@@ -64,6 +68,7 @@ <h1>Manual testing of table sort js</h1>
6468
<td>30980980b</td>
6569
<td data-sort="4">Thursday</td>
6670
<td>2/3/1879</td>
71+
<td>890.93.908</td>
6772
</tr>
6873

6974
<tr>
@@ -76,6 +81,7 @@ <h1>Manual testing of table sort js</h1>
7681
<td>902938402398b</td>
7782
<td data-sort="1">Monday</td>
7883
<td>8/6/1978</td>
84+
<td>2/3/1879</td>
7985
</tr>
8086
</table>
8187
<h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
@@ -94,7 +100,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
94100
</thead>
95101
<td class="tags"></td>
96102
<td class="category">Comedy</td>
97-
<td class="show_name">1</td>
103+
<td class="show_name">Show 1</td>
98104
<td class="ratio all" data-sort="72">18/25</td>
99105
<td class="pct all">72%</td>
100106
<td class="ratio ours" data-sort="75">3/4</td>
@@ -185,7 +191,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
185191
<td>Franklin</td>
186192
<td>Benjamin</td>
187193
<td>1706-1-17</td>
188-
<td>1</td>
194+
<td>1,000.00</td>
189195
<td>k-level</td>
190196
<td>1h 1m 17s</td>
191197
<td>10b</td>
@@ -196,7 +202,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
196202
<td>da Vinci</td>
197203
<td>Zarlo</td>
198204
<td>1452-4-15</td>
199-
<td>13000</td>
205+
<td>-9,000.21</td>
200206
<td></td>
201207
<td>1m 45s</td>
202208
<td>192038998987021b</td>
@@ -207,7 +213,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
207213
<td>Statham</td>
208214
<td>Jason</td>
209215
<td>1967-7-26</td>
210-
<td></td>
216+
<td>55,990.23</td>
211217
<td>HR</td>
212218
<td>11m 40s</td>
213219
<td>134809b</td>
@@ -218,7 +224,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
218224
<td>Micheal</td>
219225
<td>Angelo</td>
220226
<td>1958-8-21</td>
221-
<td>54</td>
227+
<td>1,000,000.23</td>
222228
<td>Marketing</td>
223229
<td>29s</td>
224230
<td>30980980b</td>
@@ -229,7 +235,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
229235
<td>Ben</td>
230236
<td></td>
231237
<td>1994/9/23</td>
232-
<td>134</td>
238+
<td>90102</td>
233239
<td>Marketing</td>
234240
<td>41s</td>
235241
<td>902938402398b</td>

public/table-sort.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
5757
function inferSortClasses(tableRows, columnIndex, column, th) {
5858
const runtimeRegex = /^(\d+h)?\s?(\d+m)?\s?(\d+s)?$/i;
5959
const fileSizeRegex = /^([.0-9]+)\s?(B|KB|KiB|MB|MiB|GB|GiB|TB|TiB)/i;
60-
// Doesn't infer dates with delimiter "."; as could capture semantic version numbers.
60+
// Don't infer dates with delimiter "."; as could capture semantic version numbers.
6161
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
6262
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
63-
const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/;
63+
// const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
64+
const numericRegex = /^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/
6465
const inferableClasses = {
6566
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
6667
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
@@ -345,6 +346,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
345346

346347
function handleNumbers(str1, str2) {
347348
let num1, num2;
349+
str1 = str1.replaceAll(",", "");
350+
str2 = str2.replaceAll(",", "");
348351
num1 = parseNumberFromString(str1);
349352
num2 = parseNumberFromString(str2);
350353

test/table.test.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,43 @@ test("dates-ymd-sort: ISO 8601 style yyyy/mm/dd; delim . or / or -", () => {
515515
});
516516
});
517517

518+
test("Sort decimals with commas", () => {
519+
expect(
520+
createTestTable(
521+
{
522+
col0: {
523+
td: [
524+
"20,000.89",
525+
"30,000.32",
526+
"1",
527+
"0.111",
528+
"21,000.92",
529+
"19845",
530+
"12000",
531+
"-90",
532+
"-10,000.39",
533+
"-10,000.10",
534+
],
535+
},
536+
},
537+
{ classTags: "numeric-sort" }
538+
)
539+
).toStrictEqual({
540+
col0: [
541+
"-10,000.39",
542+
"-10,000.10",
543+
"-90",
544+
"0.111",
545+
"1",
546+
"12000",
547+
"19845",
548+
"20,000.89",
549+
"21,000.92",
550+
"30,000.32",
551+
],
552+
});
553+
});
554+
518555
test("Sort decimal numbers", () => {
519556
expect(
520557
createTestTable(
@@ -527,7 +564,7 @@ test("Sort decimal numbers", () => {
527564
col0: ["0.1", "0.11", "0.13", "0.13", "0.14", "0.2", "0.3"],
528565
});
529566
});
530-
567+
//
531568
test("Sort all combination positive, negative numbers with parenthesis as well", () => {
532569
expect(
533570
createTestTable(
@@ -540,7 +577,7 @@ test("Sort all combination positive, negative numbers with parenthesis as well",
540577
col0: ["-6", "-3", "-2.3", "(1.4)", "1", "1.05", "14"],
541578
});
542579
});
543-
580+
//
544581
test("Sort all combination of negative and positive integers and decimal numbers and even alphabetical random", () => {
545582
expect(
546583
createTestTable(

0 commit comments

Comments
 (0)