So if i have an array of objects such as:
var a = [
{
name: "Apple",
rank: 25
},
{
name: "Cherry",
rank: 29
},
{
name: "Grape",
rank: 15
}
]
Is there a way to sort the values say by rank, by calling a function, i have had an attempt at it but keep getting undefined:
function sorting(obj){
obj.sort(function(a, b){
return a[1] - b[1];
})
}
I am struggling to find out where i am going wrong, and am unable to find any documents on MDN regarding this.
Any help would be much appreciated, thanks in advance
return a["rank"] - b["rank"];