I want to make a website which will be having posts (users can like or dislike the posts). I want to sort the posts combined with their freshness and as liked by users.But below is the problem.
So I want an algorithm (or a wordpress plugin as I am using wordpress) so that I can display the combination of new and most liked contents on my website.
You don't want an algorithm but you want to directives to design your own algorithm.
Here is what you are going to do:
This kind of algorithms can have many use-cases (recommendation of a video using its matching score with a user according to the video's attributes, the user's history, similar users feedback on the video, etc...) and designing by yourself will teach you a lot.
Good luck.
Edit: That's more the pure PHP way than wordpress, thanks to Paul for posting the wordpress way.
Try to define more specific rules, for how the posts are sorted. In your case, you want to sort descending by the number of posts and ascending by the age of the post.
define compare:
input: date ageA , date ageB , int likesA , int likes B
output: int
double ageFactor
double likesFactor
long ageA = diffSeconds(today() , ageA)
long ageB = diffSeconds(today() , ageB)
return (int) ((likesA - likesB) * likesFactor + (ageA - ageB) * ageFactor)
Basically all you'd have to do is to find the right values for ageFactor
and likesFactor
to balance the influence of likes and age of a post on it's position. The resulting int
would be positive, if post A (ageA
and likesA
) should be further up than post B.