Skip to content

Commit b28ba53

Browse files
Fix board date sorting on downloads page
The sort comparator had several issues causing date sorting to produce unexpected results: 1. Exact tag match promotion ran before the sort type switch for all sort modes, overriding date and alphabetical sorts. Tag match promotion now only applies to the default (downloads) sort. 2. The tag match only checked item 'a' and returned -2, not comparing 'b'. Both items are now compared symmetrically. 3. Date and download count comparators used ternary conditional returns instead of subtraction, never returning 0 for equal values. Changed to use arithmetic subtraction for proper three-way comparison. 4. The filterResults tag-match DOM reordering also ignored the current sort selection. Now respects the active sort choice. Fixes #1760
1 parent 17c2dcc commit b28ba53

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

assets/javascript/downloads.js

Lines changed: 18 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,30 @@ 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;
395+
var dateA = new Date(a.dataset.date);
396+
var dateB = new Date(b.dataset.date);
397+
return dateA.getTime() - dateB.getTime();
394398
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;
399+
var dateA = new Date(a.dataset.date);
400+
var dateB = new Date(b.dataset.date);
401+
return dateB.getTime() - dateA.getTime();
398402
default:
399403
// sort by download count is the default
400-
return parseInt(a.dataset.downloads, 10) < parseInt(b.dataset.downloads, 10) ? 1 : -1;
404+
return parseInt(b.dataset.downloads, 10) - parseInt(a.dataset.downloads, 10);
401405
}
402406
})
403407
.forEach(function (download) { downloads.appendChild(download); });

0 commit comments

Comments
 (0)