Implement multi-stage sorting and add upvote based sortfunc

pull/322/head
Danilo Bargen 7 years ago
parent 9b36118da6
commit 76d8ab3f23

@ -129,7 +129,24 @@ These classes can then be used to customize the appearance of comments (eg. put
data-isso-sorting
-----------------
This can be set either to `oldest` (the default) or `newest`.
A comma-separated list of thread sorting methods that are applied in the
specified order.
Possible sorting methods:
- `newest`: Bring newest comments to the top
- `oldest`: Bring oldest comments to the top
- `upvotes`: Bring most liked comments to the top
You can combine sorting methods. If you specify `upvotes,newest`, then comments
will be sorted by upvotes, and comments with the same number of upvotes will be
sorted by date (newest first).
Note that only the top level threads are sorted according to this
configuration. The thread replies are still sorted in chronological order
(oldest first), so that they can be read from top to bottom.
Default sorting is `oldest`.
data-isso-id
------------

@ -43,9 +43,37 @@ require(["app/lib/ready", "app/config", "app/i18n", "app/api", "app/isso", "app/
var lastcreated = 0;
var count = rv.total_replies;
// Correct sorting of comments
if (config["sorting"] === "newest") {
rv.replies.reverse();
// Sorting functions
var sortfuncs = {
oldest: function(a, b) { return a.created - b.created; },
newest: function(a, b) { return b.created - a.created; },
upvotes: function(a, b) { return b.likes - a.likes; },
};
// Handle sorting configuration
var sort_by = config["sorting"].split(",")
.map(function(x) { return x.trim(); })
.filter(function(x) { return x; });
function sortfunc(a, b) {
var i = 0;
while (true) {
var func = sortfuncs[sort_by[i]];
if (func === undefined) {
console.warn("Invalid sorting mode:", sort_by[i]);
return sortfuncs.oldest(a, b);
}
var compared = func(a, b);
if (compared == 0 && sort_by[i + 1] !== undefined) {
i++;
} else {
return compared;
}
}
}
if (sort_by.length === 0) {
console.warn("Invalid sorting config:", config["sorting"]);
} else {
rv.replies.sort(sortfunc);
}
rv.replies.forEach(function(comment) {

Loading…
Cancel
Save