functions/index.js
function sortResults(val, results) {
  const sortPopularity = (a, b) => {
    if (a.reviews.count < b.reviews.count) {
      return 1;
    } else if (a.reviews.count > b.reviews.count) {
      return -1;
    }
    return 0;
  };

  const sortRating = (a, b) => {
    if (a.reviews.averageRating.value < b.reviews.averageRating.value) {
      return 1;
    } else if (a.reviews.averageRating.value > b.reviews.averageRating.value) {
      return -1;
    }

    return sortPopularity(a, b);
  };

  const sortPrice = (a, b) => {
    if (a.price.value > b.price.value) {
      return 1;
    } else if (a.price.value < b.price.value) {
      return -1;
    }

    return sortRating(a, b);
  };

  const sortNew = (a, b) => {
    if (!a.flags.new && b.flags.new) {
      return 1;
    } else if (a.flags.new && !b.flags.new) {
      return -1;
    }

    return sortRating(a, b);
  };

  switch (val.toLowerCase()) {
    case "popularity-desc":
      return results.slice().sort(sortPopularity);

    case "rating-desc":
      return results.slice().sort(sortRating);

    case "age-asc":
      return results.slice().sort(sortNew);

    case "price-asc":
      return results.slice().sort(sortPrice);

    default:
      return results;
  }
}
function sortResults(val, results) {
  const sortPopularity = (a, b) => {
    if (a.reviews.count < b.reviews.count) {
      return 1;
    } else if (a.reviews.count > b.reviews.count) {
      return -1;
    }
    return 0;
  };

  const sortRating = (a, b) => {
    if (a.reviews.averageRating.value < b.reviews.averageRating.value) {
      return 1;
    } else if (a.reviews.averageRating.value > b.reviews.averageRating.value) {
      return -1;
    }

    return sortPopularity(a, b);
  };

  const sortPrice = (a, b) => {
    if (a.price.value > b.price.value) {
      return 1;
    } else if (a.price.value < b.price.value) {
      return -1;
    }

    return sortRating(a, b);
  };

  const sortNew = (a, b) => {
    if (!a.flags.new && b.flags.new) {
      return 1;
    } else if (a.flags.new && !b.flags.new) {
      return -1;
    }

    return sortRating(a, b);
  };

  switch (val.toLowerCase()) {
    case "popularity-desc":
      return results.slice().sort(sortPopularity);

    case "rating-desc":
      return results.slice().sort(sortRating);

    case "age-asc":
      return results.slice().sort(sortNew);

    case "price-asc":
      return results.slice().sort(sortPrice);

    default:
      return results;
  }
}
function sortResults ( val , results ) { const sortPopularity = ( a , b ) => { if ( a . reviews . count < b . reviews . count ) { return 1 ; } else if ( a . reviews . count > b . reviews . count ) { return - 1 ; } return 0 ; } ; const sortRating = ( a , b ) => { if ( a . reviews . averageRating . value < b . reviews . averageRating . value ) { return 1 ; } else if ( a . reviews . averageRating . value > b . reviews . averageRating . value ) { return - 1 ; } return sortPopularity ( a , b ) ; } ; const sortPrice = ( a , b ) => { if ( a . price . value > b . price . value ) { return 1 ; } else if ( a . price . value < b . price . value ) { return - 1 ; } return sortRating ( a , b ) ; } ; const sortNew = ( a , b ) => { if ( ! a . flags . new && b . flags . new ) { return 1 ; } else if ( a . flags . new && ! b . flags . new ) { return - 1 ; } return sortRating ( a , b ) ; } ; switch ( val . toLowerCase ( ) ) { case "popularity-desc" : return results . slice ( ) . sort ( sortPopularity ) ; case "rating-desc" : return results . slice ( ) . sort ( sortRating ) ; case "age-asc" : return results . slice ( ) . sort ( sortNew ) ; case "price-asc" : return results . slice ( ) . sort ( sortPrice ) ; default : return results ; } }
sortResults checks to see if the value is one of the parameters we
know how to sort by.

@param {String} val - The value to check.
@param {Array} results - Array of object to sort.
@return {Array} The sorted results.
sortResults checks to see if the value is one of the parameters we know how to sort by .