From d70ccb186880cfaa3d9b85f47b00a219cc63eff5 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 26 May 2017 17:20:17 -0400 Subject: [PATCH] closes #5708 --- src/topics/popular.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/topics/popular.js b/src/topics/popular.js index e6d78ad4b4..7d756a1548 100644 --- a/src/topics/popular.js +++ b/src/topics/popular.js @@ -9,7 +9,13 @@ module.exports = function (Topics) { count = parseInt(count, 10) || 20; if (term === 'alltime') { - return getAllTimePopular(uid, count, callback); + return getAllTimePopular(uid, count, function (err, topics) { + if (err) { + return callback(err); + } + + sortTiedTopicsByViews(topics, callback); + }); } async.waterfall([ @@ -19,6 +25,9 @@ module.exports = function (Topics) { function (tids, next) { getTopics(tids, uid, count, next); }, + function (topics, next) { + sortTiedTopicsByViews(topics, next); + }, ], callback); }; @@ -48,4 +57,12 @@ module.exports = function (Topics) { }, ], callback); } + + function sortTiedTopicsByViews(topics, next) { + topics.sort(function (a, b) { + return parseInt(a.postcount) !== parseInt(b.postcount) ? 0 : parseInt(b.viewcount, 10) - parseInt(a.viewcount, 10); + }); + + next(null, topics); + } };