In my dataset, I have an array of objects representing various products. Each product object includes a field called ratingReviews
and another field called price
, which can either be a string or an object. If the price is represented as an object, it contains a field named newUan
to hold the new price.
The ratingReviews
field consists of a number followed by a word, while the price
or newUan
field contains a human-readable number followed by a currency name in the format of space-delineated values.
I am looking to implement two functions that sort the products based on:
- The numeric value in the
ratingReviews
field. - The numeric value in the
price
field, preferringprice.newUan
if available.
--
I already know how to sort based on ratingReviews
:
const sortedByRating = () => {
return products.sort((a, b) => {
return parseFloat(b.ratingReviews) - parseFloat(a.ratingReviews)
})
}
However, I need assistance sorting based on either price
or price.newUan
depending on what's provided. Could you please provide me with guidance on that?
For reference, here is a sample of my data:
const products = [
// Sample product objects
]