const products = [ { name: 'Grapes', price: 2 }, { name: 'Banana', price: 1 }, { name: 'Apple', price: 3 }, { name: 'Cherry', price: 2 }, { name: 'Date', price: 1 } ]; products.sort((a, b) => a.price > b.price);
When using the Array.sort method, the compare function should return a number, not a boolean. Returning a negative number will sort the first item before the second, a positive number will sort the second item before the first, and returning 0 will leave the items in the same order.
const products = [ { name: 'Grapes', price: 2 }, { name: 'Banana', price: 1 }, { name: 'Apple', price: 3 }, { name: 'Cherry', price: 2 }, { name: 'Date', price: 1 } ]; products.sort((a, b) => a.price - b.price);