I have the following array of objects that I want to order by name key. For some reason if I try to sort by the "name" key it doesn't sort. If I sort by a "price_value" key it works .
let arr = [
{
"name": "Road Bike",
"slug": "road-bike",
"price_value": 2499
},
{
"name": "Laptop",
"slug": "laptop",
"price_value": 1299,
},
];
let br = [...arr].sort((a, b) => a.name - b.name)[0].name;
console.log(br) //should be Laptop, even if you try to sort on slug key doesn't work
I always get Road Bike as first element. If I try to sort on price_value key it works normally.
let br = [...arr].sort( (a,b) => a.price_value - b.price_value )[0].name;
console.log( br ) //It's Laptop as expected
Where is the problem?