MakePrisms / bitcoinmints

List, review, and find bitcoin mints.
https://bitcoinmints.com
17 stars 4 forks source link

Mints table sorting code #18

Closed minibits-cash closed 1 month ago

minibits-cash commented 1 month ago

Hi, thank you for the great project. I've briefly look at the code redering the mints table and I think that there is a small glitch when sorting the results (I assume that the goal is to sort the mints by rating weighted by number of reviews).

Current code:

                  .sort((a, b) => {
                    const aRating =
                      a.reviewsWithRating *
                        (a.totalRatings / a.reviewsWithRating / 5) || 0;
                    const bRating =
                      b.reviewsWithRating *
                        (b.totalRatings / b.reviewsWithRating / 5) || 0;

seems to always reduce a.reviewsWithRating / a.reviewsWithRating to 1 so that the whole formula becomes a.totalRatings / 5. I'd suggest to fix it to something similar to (untested):

const sortedMintInfos = mintInfos.sort((a, b) => {
  const aRating = (a.totalRatings / a.reviewsWithRating) * a.reviewsWithRating;
  const bRating = (b.totalRatings / b.reviewsWithRating) * b.reviewsWithRating;
  if (!ratingSort) return 0;
  if (ratingSort === "asc") {
    return aRating - bRating;
  } else {
    return bRating - aRating;
  }
});

<Table.Body>
  {sortedMintInfos
    .slice(mintsPage * maxPerPage - maxPerPage, mintsPage * maxPerPage)
    .map((mint, idx) => (
      <MintsRowItem mint={mint} key={idx} />
    ))}
</Table.Body>
gudnuf commented 1 month ago

hey thanks for the issue! There are two issues I see here. One is that the I was sorting the items on each page rather than sorting against all items. I just fixed that here.

The other problem is the weighted average was not being calculated correctly. I don't think your solution is correct either though because const aRating = (a.totalRatings / a.reviewsWithRating) * a.reviewsWithRating; reduces to just totalRatings. reviewsWithRatings is the number of reviews on each mint where number of ratings is defined and totalRatings is the sum of all the stars on that mint.

minibits-cash commented 1 month ago

Ah I see I've messed it. Thank you for your time to fix it.