All files / utils leaderboardSortingUtils.js

100% Statements 6/6
100% Branches 3/3
100% Functions 6/6
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34        4x   49x                 4x   46x                   4x   52x        
export function sortByWealth(
  farmerArray,
  returnArraySize = farmerArray.length,
) {
  return farmerArray
    .sort((a, b) => {
      return b.totalWealth - a.totalWealth;
    })
    .slice(0, returnArraySize);
}
 
export function sortByNumCows(
  farmerArray,
  returnArraySize = farmerArray.length,
) {
  return farmerArray
    .sort((a, b) => {
      return b.numOfCows - a.numOfCows;
    })
    .slice(0, returnArraySize);
}
 
export function sortByCowHealth(
  farmerArray,
  returnArraySize = farmerArray.length,
) {
  //sorts in decreasing order, so the comparison function returns a negative when the first parameter is larger
  return farmerArray
    .sort((a, b) => {
      return b.cowHealth - a.cowHealth;
    })
    .slice(0, returnArraySize);
}