@@ -70,25 +70,30 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
70
70
for ( let [ columnIndex , th ] of tableHeadHeaders . entries ( ) ) {
71
71
const regexMinutesAndSeconds = / ^ ( \d + h ) ? \s ? ( \d + m ) ? \s ? ( \d + s ) ? $ / i;
72
72
const regexFileSizeSort = / ^ ( [ . 0 - 9 ] + ) \s ? ( B | K B | K i B | M B | M i B | G B | G i B | T B | T i B ) / i;
73
+ const regexDates = / ^ ( \d \d ? ) [ \/ \. - ] ( \d \d ? ) [ \/ \. - ] ( ( \d \d ) ? \d \d ) $ / ;
73
74
let runtimeSortCounter = 0 ,
74
- fileSizeSortCounter = 0 ;
75
-
75
+ fileSizeSortCounter = 0 ,
76
+ datesSortCounter = 0 ;
76
77
let tableColumnLength = th . parentElement . childElementCount ;
77
78
for ( let tr of tableRows ) {
78
- let runtimeSortMatch , fileSizeSortMatch ;
79
+ let runtimeSortMatch , fileSizeSortMatch , datesSortMatch ;
79
80
const tableColumn = tr . querySelectorAll ( "td" ) . item ( columnIndex ) ;
80
81
if ( tableColumn . innerText ) {
81
82
runtimeSortMatch = tableColumn . innerText . match (
82
83
regexMinutesAndSeconds
83
84
) ;
84
85
fileSizeSortMatch = tableColumn . innerText . match ( regexFileSizeSort ) ;
86
+ datesSortMatch = tableColumn . innerText . match ( regexDates ) ;
85
87
}
86
88
if ( runtimeSortMatch ) {
87
89
runtimeSortCounter ++ ;
88
90
}
89
91
if ( fileSizeSortMatch ) {
90
92
fileSizeSortCounter ++ ;
91
93
}
94
+ if ( datesSortMatch ) {
95
+ datesSortCounter ++ ;
96
+ }
92
97
}
93
98
// TODO: refactor this into one function called addInferredClasses that loops over sort classes and counters
94
99
addInferredClass (
@@ -103,6 +108,12 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
103
108
fileSizeSortCounter ,
104
109
"file-size-sort"
105
110
) ;
111
+ addInferredClass (
112
+ th ,
113
+ tableColumnLength ,
114
+ datesSortCounter ,
115
+ "dates-dmy-sort"
116
+ ) ;
106
117
}
107
118
}
108
119
@@ -190,7 +201,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
190
201
columnOfTd = tr . querySelectorAll ( "td" ) . item ( columnIndex ) . innerText ;
191
202
}
192
203
let match = columnOfTd . match ( regexMinutesAndSeconds ) ;
193
- let [ minutesInSeconds , hours , seconds , timeinSeconds ] = [ 0 , 0 , 0 , 0 ] ;
204
+ let [ minutesInSeconds , hours , seconds ] = [ 0 , 0 , 0 ] ;
205
+ let timeinSeconds = columnOfTd ;
194
206
if ( match ) {
195
207
const regexHours = match [ 1 ] ;
196
208
if ( regexHours ) {
@@ -214,6 +226,45 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
214
226
}
215
227
}
216
228
229
+ function sortDates ( dateFormat , tableRows , columnData ) {
230
+ try {
231
+ for ( let [ i , tr ] of tableRows . entries ( ) ) {
232
+ let columnOfTd ;
233
+ const regexDate = / ^ ( \d \d ? ) [ . / - ] ( \d \d ? ) [ . / - ] ( ( \d \d ) ? \d \d ) $ / ;
234
+ columnOfTd = tr . querySelectorAll ( "td" ) . item ( columnIndex ) . textContent ;
235
+ let match = columnOfTd . match ( regexDate ) ;
236
+ let [ years , days , months ] = [ 0 , 0 , 0 ] ;
237
+ let numberToSort = columnOfTd ;
238
+ if ( match ) {
239
+ const regexFirstNumber = match [ 1 ] ;
240
+ const regexSecondNumber = match [ 2 ] ;
241
+ const regexYears = match [ 3 ] ;
242
+ if ( regexFirstNumber && regexSecondNumber ) {
243
+ if ( dateFormat === "mdy" ) {
244
+ days = regexSecondNumber ;
245
+ months = regexFirstNumber ;
246
+ } else {
247
+ days = regexFirstNumber ;
248
+ months = regexSecondNumber ;
249
+ }
250
+ }
251
+ if ( regexYears ) {
252
+ years = regexYears ;
253
+ }
254
+ numberToSort = Number (
255
+ years +
256
+ String ( months ) . padStart ( 2 , "0" ) +
257
+ String ( days ) . padStart ( 2 , "0" )
258
+ ) ;
259
+ }
260
+ columnData . push ( `${ numberToSort } #${ i } ` ) ;
261
+ columnIndexAndTableRow [ columnData [ i ] ] = tr . innerHTML ;
262
+ }
263
+ } catch ( e ) {
264
+ console . log ( e ) ;
265
+ }
266
+ }
267
+
217
268
let [ timesClickedColumn , columnIndexesClicked ] = [ 0 , [ ] ] ;
218
269
219
270
function rememberSort ( timesClickedColumn , columnIndexesClicked ) {
@@ -245,6 +296,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
245
296
columnData,
246
297
isFileSize,
247
298
isTimeSort,
299
+ isSortDateDayMonthYear,
300
+ isSortDateMonthDayYear,
248
301
isDataAttribute,
249
302
colSpanData,
250
303
colSpanSum,
@@ -264,7 +317,14 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
264
317
if ( isFileSize ) {
265
318
fileSizeColumnTextAndRow [ columnData [ i ] ] = tr . innerHTML ;
266
319
}
267
- if ( ! isFileSize && ! isDataAttribute && ! isTimeSort ) {
320
+ // These classes already handle pushing to column and setting the tr html.
321
+ if (
322
+ ! isFileSize &&
323
+ ! isDataAttribute &&
324
+ ! isTimeSort &&
325
+ ! isSortDateDayMonthYear &&
326
+ ! isSortDateMonthDayYear
327
+ ) {
268
328
columnData . push ( `${ tdTextContent } #${ i } ` ) ;
269
329
columnIndexAndTableRow [ `${ tdTextContent } #${ i } ` ] = tr . innerHTML ;
270
330
}
@@ -405,6 +465,15 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
405
465
sortByRuntime ( visibleTableRows , columnData ) ;
406
466
}
407
467
468
+ const isSortDateDayMonthYear = th . classList . contains ( "dates-dmy-sort" ) ;
469
+ const isSortDateMonthDayYear = th . classList . contains ( "dates-mdy-sort" ) ;
470
+ // pick mdy first to override the inferred default class which is dmy.
471
+ if ( isSortDateMonthDayYear ) {
472
+ sortDates ( "mdy" , visibleTableRows , columnData ) ;
473
+ } else if ( isSortDateDayMonthYear ) {
474
+ sortDates ( "dmy" , visibleTableRows , columnData ) ;
475
+ }
476
+
408
477
const isRememberSort = sortableTable . classList . contains ( "remember-sort" ) ;
409
478
if ( ! isRememberSort ) {
410
479
rememberSort ( timesClickedColumn , columnIndexesClicked ) ;
@@ -417,6 +486,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
417
486
tableRows : visibleTableRows ,
418
487
columnData,
419
488
isFileSize,
489
+ isSortDateDayMonthYear,
490
+ isSortDateMonthDayYear,
420
491
isDataAttribute,
421
492
isTimeSort,
422
493
colSpanData,
0 commit comments