# The "Right" way to Sort by Rating and Recency

Let's say you have a website with user generate...

Let’s say you have a website with user generated ratings. You might think that sorting items by rating would be an easy feature to implement. Well, depending on your definition of easy, you’d likely be wrong. The crux of the issue is, what happens when you have an item that has a single 5 star rating and another that has 1000 ratings with an average of 4.5 stars?

The equation above is from this article and results in:

Given the ratings I have, there is a 95% chance that the “real” fraction of positive ratings is at least what?

The article makes a strong case that this is the “right” way to sort by rating. Read the article for implementation details.

### Combining Recency and Rating

Another not obvious sorting problem is how to combine recency with rating. This article describes how Hacker News does it.

Essentially, they add a time factor after the number of votes. This time factor is: 1/(T+2)^G

Where T is the time in hours and G is a configurable “Gravity”. The following graph shows how the algorithm treats three different scores over time.

These two methods could be combined, but I’ll leave that as an exercise for the reader.