I have 3 filters: ascending, descending (price) and newest. Price filters work just fine but the newest filter which is constructed just as the other ones, doesn't work. When clicking newest, there's no change in the products. I've console.logged sort and it shows the array. Here's my code:
console.log(sort)
if (sort === "newest") {
setFilteredProducts((prev) =>
[...prev].sort((a, b) => b.createdAt - a.createdAt)
);
} else if (sort === "descending") {
setFilteredProducts((prev) =>
[...prev].sort((a, b) => a.price - b.price)
);
} else {
setFilteredProducts((prev) =>
[...prev].sort((a, b) => b.price - a.price)
);
}
}, [sort]);
And code for my options:
<Option value="newest">Newest</Option>
<Option value="ascending">Price (High)</Option>
<Option value="descending">Price (Low)</Option>
</Select>
The default value of sort is "newest"
All products have "createdAt" property:
"_id": {
"$oid": "631608a6fac350ae5644520f"
},
"title": "north face",
"description": "trekking",
"image": "https://purepng.com/public/uploads/large/purepng.com-jacketclothingjacketfashion-men-dress-wear-cloth-coat-jacket-631522326867zvwfe.png",
"categories": [
"women",
"test2"
],
"size": [
"M",
"S"
],
"color": [
"yellow",
"green"
],
"price": 1,
"inStock": true,
"createdAt": {
"$date": {
"$numberLong": "1662388390011"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1662388390011"
}
},
"__v": 0
}```