Skip to content

Commit 4915fbd

Browse files
authored
Merge pull request #1761 from makermelissa-piclaw/fix-date-sorting
Fix board date sorting on downloads page
2 parents 34648ae + 97b3e60 commit 4915fbd

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

assets/javascript/downloads.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,10 @@ function filterResults() {
350350
} else {
351351
download.style.display = 'block';
352352
board_count++;
353-
// exact tag match re-order
354-
if (downloadsSearch.searchTerm !== null && downloadsSearch.searchTerm !== undefined) {
353+
// exact tag match re-order only for default (downloads) sort
354+
var currentSort = document.querySelector("input[name='sort-by']:checked");
355+
var currentSortValue = currentSort ? currentSort.value : 'downloads';
356+
if (currentSortValue === 'downloads' && downloadsSearch.searchTerm !== null && downloadsSearch.searchTerm !== undefined) {
355357
let searched = downloadsSearch.searchTerm.toLowerCase();
356358
let tags = download.dataset.tags?.split(",") || [];
357359
if (searched !== "" && tags.indexOf(searched) >= 0) {
@@ -376,28 +378,36 @@ function handleSortResults(event) {
376378
Array.prototype.slice.call(downloads.children)
377379
.map(function (download) { return downloads.removeChild(download); })
378380
.sort(function (a, b) {
379-
// exact tag match re-order
380-
if (searched !== undefined && searched !== "" &&
381-
a.dataset.tags.split(",").indexOf(searched) >= 0){
382-
383-
return -2;
381+
// exact tag match re-order only for default (downloads) sort
382+
if (sortType === 'downloads' && searched !== undefined && searched !== "") {
383+
var aMatch = a.dataset.tags.split(",").indexOf(searched) >= 0;
384+
var bMatch = b.dataset.tags.split(",").indexOf(searched) >= 0;
385+
if (aMatch !== bMatch) {
386+
return aMatch ? -1 : 1;
387+
}
384388
}
385389
switch(sortType) {
386390
case 'alpha-asc':
387391
return a.dataset.name.localeCompare(b.dataset.name);
388392
case 'alpha-desc':
389393
return b.dataset.name.localeCompare(a.dataset.name);
390394
case 'date-asc':
391-
dateA = new Date(a.dataset.date)
392-
dateB = new Date(b.dataset.date)
393-
return dateA.getTime() < dateB.getTime() ? -1 : 1;
394395
case 'date-desc':
395-
dateA = new Date(a.dataset.date)
396-
dateB = new Date(b.dataset.date)
397-
return dateA.getTime() < dateB.getTime() ? 1 : -1;
396+
var dateA = a.dataset.date ? new Date(a.dataset.date) : null;
397+
var dateB = b.dataset.date ? new Date(b.dataset.date) : null;
398+
var validA = dateA && !isNaN(dateA.getTime());
399+
var validB = dateB && !isNaN(dateB.getTime());
400+
// boards without a valid date go to the end
401+
if (!validA && !validB) return 0;
402+
if (!validA) return 1;
403+
if (!validB) return -1;
404+
if (sortType === 'date-asc') {
405+
return dateA.getTime() - dateB.getTime();
406+
}
407+
return dateB.getTime() - dateA.getTime();
398408
default:
399409
// sort by download count is the default
400-
return parseInt(a.dataset.downloads, 10) < parseInt(b.dataset.downloads, 10) ? 1 : -1;
410+
return parseInt(b.dataset.downloads, 10) - parseInt(a.dataset.downloads, 10);
401411
}
402412
})
403413
.forEach(function (download) { downloads.appendChild(download); });

0 commit comments

Comments
 (0)