1 # Trending / Hot / Best Sorting algorithm
3 - During the day, new posts and comments should be near the top, so they can be voted on.
4 - After a day or so, the time factor should go away.
5 - Use a log scale, since votes tend to snowball, and so the first 10 votes are just as important as the next hundred.
8 [Reddit's comment sorting algorithm](https://medium.com/hacking-and-gonzo/how-reddit-ranking-algorithms-work-ef111e33d0d9), the wilson confidence sort, is inadequate, because it completely ignores time. What ends up happening, especially in smaller subreddits, is that the early comments end up getting upvoted, and newer comments stay at the bottom, never to be seen. Research showed that nearly all top comments are just the [first ones posted.](https://minimaxir.com/2016/11/first-comment/)
10 ## Hacker News Sorting
11 The [Hacker New's ranking algorithm](https://medium.com/hacking-and-gonzo/how-hacker-news-ranking-algorithm-works-1d9b0cf2c08d) is great, but it doesn't use a log scale for the scores.
15 Rank = ScaleFactor * log(Max(1, 3 + Score)) / (Time + 2)^Gravity
17 Score = Upvotes - Downvotes
18 Time = time since submission (in hours)
19 Gravity = Decay gravity, 1.8 is default
21 - For posts, in order to bring up active posts, it uses the latest comment time (limited to a max creation age of a month ago)
22 - Use Max(1, score) to make sure all comments are affected by time decay.
23 - Add 3 to the score, so that everything that has less than 3 downvotes will seem new. Otherwise all new comments would stay at zero, near the bottom.
24 - The sign and abs of the score are necessary for dealing with the log of negative scores.
25 - A scale factor of 10k gets the rank in integer form.
27 A plot of rank over 24 hours, of scores of 1, 5, 10, 100, 1000, with a scale factor of 10k.
29 ![](https://raw.githubusercontent.com/LemmyNet/lemmy/main/docs/img/rank_algorithm.png)