I want to use cursor pagination to fetch comments from a database based on likes from most likes to least likes and load them in a comments section. This functionality is meant to mirror youtube’s ‘Top comments’. An example is that I load a set of comments and use the last comment as a cursor. Let’s say the last comment has 10 likes. The next set of comments to be loaded should have likes fewer or equal to 10 likes. A problem that occurs is that before I load the next set of comments, during this time comments that have fewer likes may have gained more likes (e.g., from 9 to 12) than the cursor and comments that I have already fetched and loaded may lose likes and make it have fewer likes (e.g., from 14 to 7) than the cursor. So when I load the next set of comments, those loaded comments may exclude comments that have recently gained likes and/or load the same comments that have already been loaded.
I tried experimenting with youtube’s ‘Top comments’ to see if it has this same problem but I don’t get duplicate or missed comments. I know youtube’s ‘Top comments’ most likely uses a complex algorithm to determine the top comments and is not just based on likes count. Is there an algorithm that exists that already solves this problem? Youtube seems to have already solved this problem but I don’t know how they do it. I can’t figure out an efficient solution to solve this problem.
Bob Bob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.