This question has been answered a thousand times, but for the life of me I can't get it to work. I've got an array products
containing, you guessed it, products. A product basically looks like this:
product{
product.product_name,
product.price,
product.amount,
product.store_name,
product.image_url
}
Not sure if that's the correct way to display it for React, but you get the idea. I want to display these products in a table. Now, I made a dropdown selector which needs to sort these. So, when someone selects, for example, "price" in the dropdown, the table with products needs to be ordered on price. At the moment I sort them like this:
if (order == 0){
sortedProducts = products.sort((a, b) => b.product_name - a.product_name);
} else if (order == 1){
sortedProducts = products.sort((a, b) => b.price - a.price);
} else if (order == 2){
sortedProducts = products.sort((a, b) => b.amount - a.amount);
} else {
sortedProducts = products.sort((a, b) => b.store_name - a.store_name);
}
var items = [];
for (var i = pageNo * pagination; i < sortedProducts.length; i++) {
[... convert to table rows ...]
}
return items;
I got the code from this SO post, and found another blog using this method. However, I don't really understand it and it doesn't work either. My first try was this:
sortedProducts = products.sort(p => p.product_name);
For the product name, but sadly this didn't work either. Does someone know how I can accomplish this?