I'm trying to understand the sort function. If I want to sort an array descending based on its values I do this :
var items = [
{ name: 'Edward', value: 21 },
{ name: 'Sharpe', value: 37 },
{ name: 'And', value: 45 },
{ name: 'The', value: -12 },
{ name: 'Magnetic', value: 13 },
{ name: 'Zeros', value: 37 }
];
// sort by value
const b = items.sort(function (a, b) {
return b.value - a.value;
});
console.log(b)
It works but I'm not sure how exactly , what does b.value represent ? and how does substracting those values sort the array.
I tried logging b.value but i got a list of numbers in an order that didnt make sense. Same with a.value.
Also tried looking up the documentation for this method but couldnt understand it. Could be language barrier or poorly explained.