All files / utils leaderboardSortingUtils.js

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

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 341x 4x 4x 4x 4x 4x 49x 4x 4x 4x   1x 4x 4x 4x 4x 4x 46x 4x 4x 4x   1x 4x 4x 4x 4x 4x 4x 52x 4x 4x 4x  
export function sortByWealth(
  userCommonsArray,
  returnArraySize = userCommonsArray.length,
) {
  return userCommonsArray
    .sort((a, b) => {
      return b.totalWealth - a.totalWealth;
    })
    .slice(0, returnArraySize);
}
 
export function sortByNumCows(
  userCommonsArray,
  returnArraySize = userCommonsArray.length,
) {
  return userCommonsArray
    .sort((a, b) => {
      return b.numOfCows - a.numOfCows;
    })
    .slice(0, returnArraySize);
}
 
export function sortByCowHealth(
  userCommonsArray,
  returnArraySize = userCommonsArray.length,
) {
  //sorts in decreasing order, so the comparison function returns a negative when the first parameter is larger
  return userCommonsArray
    .sort((a, b) => {
      return b.cowHealth - a.cowHealth;
    })
    .slice(0, returnArraySize);
}