用数字存储为文本对javascript数组进行排序

时间:2018-07-19 15:28:06

标签: javascript arrays sorting

我正在尝试对array进行排序。我正在尝试通过每个方括号中的[2]条目对块进行排序。因此,第15位应该在顶部,第8位应该排在第二,依此类推。

我尝试使用。

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

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

但这没用。是因为数字存储为文本吗?如果可以的话,我试过了

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

这也不起作用。

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"]

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

这可以解决问题

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

参考:此answer

答案 1 :(得分:1)

您可以将巨大的价值Infinity用于将未指定的项目排序到顶部。

Array#sort期望所需顺序的数值小于零,零或大于零。要获得此结果,您可以将简单的数字作为增量。

b - a // sorts descending

对于未使用的数组索引,可以使用in operator对其进行检查,并采用一个值,该值通过使用一个较大的值(通常将其用作数字)来移动或保持项目在实际位置的顶部。数组,即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; }