0

I'm trying to sorted my array. I'm trying to sort the blocks by the [2] entry in each of the square brackets. So the 15th should be at the top and the 8th should be second and so on.

I've tried using.

var sorted = data.sort(function(a,b){return b[2]-a[2]});
console.log(sorted);

and

data.sort(function(a,b){return b[2]-a[2]});
console.log(sorted);

but this didn't work. Is it because the numbers are stored as text? if so i've tried par

data.sort(function(a,b){return parseFloat(b[2])-parseFloat(a[2])});
console.log(sorted);

and this didn't work 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 help would be appreciated.

7
  • 1
    your missing the s in pareFloat. Should be parseFloat. Commented Jul 19, 2018 at 15:31
  • 1
    your array at index 1 is not having an element at index 2. what about that? Commented Jul 19, 2018 at 15:32
  • 4
    Could you also provide the original array before sorting? Commented Jul 19, 2018 at 15:32
  • 3
    You are mixing different types of records. You need to clean your data first and then sort them. Items at indexes 1 and 15 are obviously just some aggregations and the item at index 0 is some label. Commented Jul 19, 2018 at 15:36
  • @GetOffMyLawn this is the unsorted data. the things i have tried don't make a difference Commented Jul 19, 2018 at 15:42

2 Answers 2

1

You could take a huge value Infinity for sorting non given items to top.

Array#sort expects a numerical value of smaller than zero, zero or greater than zero for the wanted order. To get this result, you could take for simple numbers the delta of it.

b - a // sorts descending

For a not used index of the array, you could check it with in operator and take a value which shifts or keeps the item at the actual place on top by using a greater value then usually taken as number in the array, which is Infinity.

var data = [["HDR", "GENERATION BY FUEL TYPE CURRENT Last Updated At 20180719150500"], ["FTR", "14"], ["FUELINSTHHCUR", "OCGT", "0", "0.0", "0", "0.0", "0", "0.0"], ["FUELINSTHHCUR", "OIL", "0", "0.0", "0", "0.0", "0", "0.0"], ["FUELINSTHHCUR", "COAL", "947", "3.0", "900", "2.9", "15144", "2.2"], ["FUELINSTHHCUR", "NUCLEAR", "6407", "20.4", "6477", "20.8", "158268", "22.5"], ["FUELINSTHHCUR", "WIND", "1710", "5.4", "1678", "5.4", "11903", "1.7"], ["FUELINSTHHCUR", "PS", "165", "0.5", "174", "0.6", "6854", "1.0"], ["FUELINSTHHCUR", "CCGT", "17355", "55.2", "17144", "55.1", "382043", "54.3"], ["FUELINSTHHCUR", "OTHER", "59", "0.2", "59", "0.2", "1406", "0.2"], ["FUELINSTHHCUR", "INTFR", "1998", "6.4", "1998", "6.4", "47363", "6.7"], ["FUELINSTHHCUR", "INTIRL", "0", "0.0", "0", "0.0", "1960", "0.3"], ["FUELINSTHHCUR", "INTNED", "1001", "3.2", "1000", "3.2", "23097", "3.3"], ["FUELINSTHHCUR", "INTEW", "0", "0.0", "0", "0.0", "4803", "0.7"], ["FUELINSTHHCUR", "BIOMASS", "1649", "5.2", "1621", "5.2", "48031", "6.8"], ["TOTAL", "31448", "100.0", "31128", "100.0", "703547", "100.0"], ["FUELINSTHHCUR", "NPSHYD", "157", "0.5", "77", "0.2", "2675", "0.4"]];

data.sort((a, b) => (2 in b ? b[2] : Infinity) - (2 in a ? a[2] : Infinity));

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

could you possibly explain what this is doing to sort the data?
1

This would do your trick

var collator = new Intl.Collator(undefined, {
  numeric: true,
  sensitivity: 'base'
});
var myArray = ['1_Document', '11_Document', '2_Document'];
console.log(myArray.sort(collator.compare));

Reference: This answer

1 Comment

This kind of works but it's sorting on the [1] entry of each array. I need to sort from the [2] of each array from largest to smallest?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.