muscalu-c
7/23/2019 - 10:26 AM

sortByDiscount

const { sortBy, orderBy } = require('lodash');

const filteredGames = [
  { name: 'game2', discountActive: 1, price: 5, priceDiscounted: 4 },
  { name: 'game6', discountActive: 1, price: 20, priceDiscounted: 8 },
  { name: 'game5', discountActive: 0, price: 10, priceDiscounted: 9 },
  { name: 'game3', discountActive: 0, price: 11, priceDiscounted: 10 },
  { name: 'game4', discountActive: 1, price: 22, priceDiscounted: 17 },
  { name: 'game1', discountActive: 0, price: 21, priceDiscounted: 18 },
];

const value = game => game.discountActive === 1 ? game.priceDiscounted : game.price;
const sorted = filteredGames.sort((a, b) => value(a) - value(b));

console.log(sorted);