I am trying to sort this array by price. If the item is on sale then by its onSalePrice and if it is not sale then by its price. I have my current solution below but it doesn't work. Any thoughts??
The 'lowest-price' parameter is indicating toward the value in my option field. This parameter is definitely working okay. The issue is in the conditions that I am sorting with
const products = [
{
price: 28.99,
onSalePrice: 22.99,
onSale: "True",
},
{
price: 26.99,
onSalePrice: 22.99,
onSale: "True",
},
{
price: 24.99,
onSale: "False",
},
];
if (sort === "lowest-price") {
if (products.onSale === "True") {
tempProducts = products.slice().sort((a, b) => {
return a.onSalePrice - b.onSalePrice;
});
} else if (products.onSale === "False") {
tempProducts = products.slice().sort((a, b) => {
return a.price - b.price;
});
}
}