feat: sort by adjusted rating
based on the calculation here: https://stats.stackexchange.com/questions/6418/rating-system-taking-account-of-number-of-votes/6423#6423
This commit is contained in:
@@ -345,8 +345,9 @@
|
|||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
if (a.score === b.score) {
|
if (a.score === b.score) {
|
||||||
if (a.rating === b.rating) return 0;
|
// sort matching search score by adjusted rating
|
||||||
return a.rating > b.rating ? -1 : 1;
|
if (a.rating_adjusted === b.rating_adjusted) return 0;
|
||||||
|
return a.rating_adjusted > b.rating_adjusted ? -1 : 1;
|
||||||
}
|
}
|
||||||
return a.score > b.score ? -1 : 1;
|
return a.score > b.score ? -1 : 1;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -18,10 +18,25 @@ async function build() {
|
|||||||
const data = await getData();
|
const data = await getData();
|
||||||
const options = {};
|
const options = {};
|
||||||
|
|
||||||
|
// calculate adjusted ratings
|
||||||
|
const totalMean =
|
||||||
|
data.strains.reduce((acc, strain) => acc + strain.rating + strain.rating_count, 0) /
|
||||||
|
data.strains.length;
|
||||||
|
|
||||||
|
data.strains = data.strains.map(strain => {
|
||||||
|
const minRatings = 10;
|
||||||
|
const { rating, rating_count: count } = strain;
|
||||||
|
return Object.assign(strain, {
|
||||||
|
rating_adjusted:
|
||||||
|
(((count / (count + minRatings)) * rating) / (minRatings / (count + minRatings))) *
|
||||||
|
totalMean,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// order strains by rating
|
// order strains by rating
|
||||||
data.strains = data.strains.sort((n, strain) => {
|
data.strains = data.strains.sort((n, strain) => {
|
||||||
if (strain.rating === n.rating) return 0;
|
if (strain.rating_adjusted === n.rating_adjusted) return 0;
|
||||||
return strain.rating < n.rating ? -1 : 1;
|
return strain.rating_adjusted < n.rating_adjusted ? -1 : 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user