I'd like to sort this array using column 5 (Pts).
var table=[
["teamA",6,2,0,2,6],
["teamB",6,1,1,2,4],
["teamC",6,2,1,1,7]];
It's a football league table with Pld, W, D ,L and Pts columns. I plan to add goal difference later.
I've attempted the code below:
console.log(table.sort(compare));
function compare( a, b ) {
if (table[a][5]<table[b][5]){
return -1;
}
if (table[a][5]>table[b][5]){
return 1;
}
return 0;
}
Unfortunately the code doesn't even run. I get the error
cannot read property '5' of undefined.