I'm attempting to organize my array
. My goal is to sort the elements based on the value within the square brackets at index 2. This means that the element at position 15 should be at the top, followed by the one at position 8 and so forth.
I've made attempts using:
var sorted = data.sort(function(a,b){return b[2]-a[2]});
console.log(sorted);
as well as:
data.sort(function(a,b){return b[2]-a[2]});
console.log(sorted);
However, none of these methods seemed to work. Could it be due to the numbers being stored as text? If so, I also tried using:
data.sort(function(a,b){return parseFloat(b[2])-parseFloat(a[2])});
console.log(sorted);
but unfortunately, this didn't produce the desired result either.
0:(2) ["HDR", "GENERATION BY FUEL TYPE CURRENT Last Updated At 20180719150500"]
1:(2) ["FTR", "14"]
2:(8) ["FUELINSTHHCUR", "OCGT", "0", "0.0", "0", "0.0", "0", "0.0"]
3:(8) ["FUELINSTHHCUR", "OIL", "0", "0.0", "0", "0.0", "0", "0.0"]
4:(8) ["FUELINSTHHCUR", "COAL", "947", "3.0", "900", "2.9", "15144", "2.2"]
5:(8) ["FUELINSTHHCUR", "NUCLEAR", "6407", "20.4", "6477", "20.8", "158268", "22.5"]
6:(8) ["FUELINSTHHCUR", "WIND", "1710", "5.4", "1678", "5.4", "11903", "1.7"]
7:(8) ["FUELINSTHHCUR", "PS", "165", "0.5", "174", "0.6", "6854", "1.0"]
8:(8) ["FUELINSTHHCUR", "CCGT", "17355", "55.2", "17144", "55.1", "382043", "54.3"]
9:(8) ["FUELINSTHHCUR", "OTHER", "59", "0.2", "59", "0.2", "1406", "0.2"]
10:(8) ["FUELINSTHHCUR", "INTFR", "1998", "6.4", "1998", "6.4", "47363", "6.7"]
11:(8) ["FUELINSTHHCUR", "INTIRL", "0", "0.0", "0", "0.0", "1960", "0.3"]
12:(8) ["FUELINSTHHCUR", "INTNED", "1001", "3.2", "1000", "3.2", "23097", "3.3"]
13:(8) ["FUELINSTHHCUR", "INTEW", "0", "0.0", "0", "0.0", "4803", "0.7"]
14:(8) ["FUELINSTHHCUR", "BIOMASS", "1649", "5.2", "1621", "5.2", "48031", "6.8"]
15:(7) ["TOTAL", "31448", "100.0", "31128", "100.0", "703547", "100.0"]
16:(8) ["FUELINSTHHCUR", "NPSHYD", "157", "0.5", "77", "0.2", "2675", "0.4"]
Any assistance would be greatly appreciated.