Skip to content

Instantly share code, notes, and snippets.

@muscaiu
Created July 23, 2019 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muscaiu/11f2f9ce9590bc075ef41d6c3d2fa0b2 to your computer and use it in GitHub Desktop.
Save muscaiu/11f2f9ce9590bc075ef41d6c3d2fa0b2 to your computer and use it in GitHub Desktop.
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment