Created
July 18, 2014 08:58
-
-
Save noodlehaus/0df99e4016510ebfd94b to your computer and use it in GitHub Desktop.
ranking formula example -- stackoverflow.com/questions/16168727/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_rank($votes_min, $votes_item, | |
$rating_average_global, $rating_average_item) { | |
return | |
($votes_item / ($votes_item + $votes_min)) * $rating_average_item + | |
($votes_min / ($votes_item + $votes_min)) * $rating_average_global; | |
} | |
$data = array( | |
[5, 10], | |
[55, 8], | |
[76, 5], | |
[8, 10], | |
[102, 2] | |
); | |
$votes_min = 10; | |
$votes_total = 0; | |
$rating_average_global = 0; | |
foreach ($data as $row) { | |
$rating_average_global += ($row[0] * $row[1]); | |
$votes_total += $row[0]; | |
} | |
$rating_average_global /= $votes_total; | |
$results = array(); | |
foreach ($data as $row) { | |
$results[] = array( | |
get_rank($votes_min, $row[0], $rating_average_global, $row[1]), | |
$row[0], | |
$row[1] | |
); | |
} | |
usort($results, function ($a, $b) { | |
if ($a[0] === $b[0]) | |
return 0; | |
if ($a[0] < $b[0]) | |
return 1; | |
return -1; | |
}); | |
foreach ($results as $row) | |
printf("rank - %3.2f, votes - %3d, avg - %3d\n", $row[0], $row[1], $row[2]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment