I am having trouble sorting the 2-dimensional array based on column values. Here is the array:
var cl12 = [[9, 10.5], [10, 11.5], [12, 13.5], [12.5, 14.5], [14.5, 15], [16, 18], [16, 17]]
In the above array, you can see that the last two elements have the same value for the 0th index, but they are not sorted correctly based on column values. For example, [16,18] should come after [16,17]. I tried using a for loop to sort the array completely like this:
for(i=0;i<cl12.length;i++){
if((cl12[i][0]==cl12[i+1][0]) && (cl12[i][1]>cl12[i+1][1])){
var temp = cl12[i+1];
cl12[i+1]=cl12[i];
cl12[i] = temp ;
}
}
console.log(cl12)
However, when I run this code, I get an error in the console: Uncaught TypeError: Cannot read property '0' of undefined"