Skip to content

Commit

Permalink
Sort-reactions: Correct unsorted comment order. Fixes #42
Browse files Browse the repository at this point in the history
  • Loading branch information
Mottie committed Apr 12, 2018
1 parent 084de0b commit 60ab556
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions github-sort-reactions.user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==UserScript==
// @name GitHub Sort Reactions
// @version 0.2.2
// @version 0.2.3
// @description A userscript that sorts comments by reaction
// @license MIT
// @author Rob Garrison
Expand Down Expand Up @@ -93,10 +93,10 @@ thumbs down = -1)" data-sort="ACTIVE">
0;
}

function extractSortValue(elm, type) {
function extractSortValue(elm, type, dir) {
if (type === "NONE" || type === "ACTIVE") {
return parseFloat(
elm.dataset[`sortComment${type === "NONE" ? "Date" : "Sum"}`]
elm.dataset[`sortComment${dir === 0 ? "Date" : "Sum"}`]
);
}
return getValue($(`.comment-reactions button[value*="${type}"]`, elm));
Expand Down Expand Up @@ -136,20 +136,20 @@ thumbs down = -1)" data-sort="ACTIVE">
container = $(".js-discussion"),
sortBlock = $(".ghsc-sort-block"),
loadMore = $("#progressive-timeline-item-container"),
dir = currentSort.dir !== 1,
dir = currentSort.dir,
sortAsc = dir !== 1,
type = currentSort.el ? currentSort.el.dataset.sort : "NONE";

currentSort.type = type;
updateAvatar();

$$(".js-timeline-item")
.sort((a, b) => {
const av = extractSortValue(a, type),
bv = extractSortValue(b, type);
const av = extractSortValue(a, type, dir),
bv = extractSortValue(b, type, dir);
if (av === bv) {
return stableSortValue(a) - stableSortValue(b);
}
return dir ? av - bv : bv - av;
return sortAsc ? av - bv : bv - av;
})
.forEach(el => {
fragment.appendChild(el);
Expand Down

0 comments on commit 60ab556

Please sign in to comment.