I'm working with an array of posts, each containing data on 'views' and 'likes', along with the user IDs associated with those likes. My goal is to sort this array based on the like rate.
However, my current approach seems to be ineffective.
posts.sort(function (a, b) {
return (b.likes.length * 100) / b.views.length
});
{
"title":"Post 1",
"views":"[1, 2, 3, 4, 5, 6]",
"likes":"[1, 2]"
}
{
"title":"Post 2",
"views":"[1, 2, 3, 4, 5, 6]",
"likes":"[1, 2, 3, 4, 5, 6]"
}
{
"title":"Post 3",
"views":"[1, 2, 3, 4, 5, 6, 7, 8, 9]",
"likes":"[1, 2, 3, 4, 5]"}
}]
Does anyone have any suggestions or solutions?