简体   繁体   English

如何对多维数组中的多列数据进行排序?

[英]How to sort multiple columns data in Multi-Dimensional array?

I am working with a Javascript code where I have an array like below: 我正在使用Javascript代码,其中有一个如下数组:

Array: 数组:

[
    ["2015-08-09", 1.2, 1.20, 2.1],
    ["2015-08-10", 1.2, 1.21, 2.11],
    ["2015-08-11", 1.2, 0.99, 2.20],
    ["2015-10-29", 1.2, 1.12, 2.22],
    ["2015-09-10", 1.21, 1.19, 2.00]
]

My required Result is: 我要求的结果是:

[
    ["2015-08-09", 1.20, 1.20, 2.10],
    ["2015-08-10", 1.20, 1.21, 2.11],
    ["2015-08-11", 1.20, 0.99, 2.20],
    ["2015-10-29", 1.20, 1.12, 2.22],
    ["2015-09-10", 1.21, 1.19, 2.00]
]

As the array is 3 dimensional I want to sort on column 1 and 2 , so first apply ascending on column 1 and then column 2 (Ascending/descending). 由于阵列是三维欲排序column 12 ,所以第一开柱申请升序1 ,然后柱2 (升序/降序)。

Thanks In Advance. 提前致谢。

I also face the same issue and found a good solution for sort multiple columns data in Multi-Dimensional array. 我也面临同样的问题,并找到了一种对多维数组中的多列数据进行排序的好方法。

Check the below code 检查以下代码

 (function() { function deepsort(){ var i, order= arguments, L= order.length, tem; return a.sort(function(a, b){ i= 0; while(i < L){ tem= order[i++]; var res = tem.split("_"); var ao= a[res[0]] || 0, bo= b[res[0]] || 0; if(ao== bo) continue; if(res[1] == "ASC"){ return ao > bo? 1: -1; } if(res[1] == "DESC"){ return ao < bo? 1: -1; } } return 0; }); } var a= [ ["2015-08-09", 1.2, 1.20, 2.1], ["2015-08-10", 1.2, 1.21, 2.11], ["2015-08-11", 1.2, 0.99, 2.20], ["2015-10-29", 1.2, 1.12, 2.22], ["2015-09-10", 1.21, 1.19, 2.00] ]; document.write(deepsort(1+"_ASC",2+"_ASC")); // for better result view check console log console.log(deepsort(1+"_ASC",2+"_ASC")) //console.log(a.deepsort(1)) })(); 

Here you have another approach using a more like mathematical expression on the sort comparison function, and also formats the output as you expected: 在这里,您可以使用另一种方法在排序比较函数上使用更类似的数学表达式,并按预期设置输出的格式:

 const input = [ ["2015-08-09", 1.2, 1.20, 2.1], ["2015-08-10", 1.2, 1.21, 2.11], ["2015-08-11", 1.2, 0.99, 2.20], ["2015-10-29", 1.2, 1.12, 2.22], ["2015-09-10", 1.21, 1.19, 2.00] ]; // First, we use map() to clone the array so we don't change // the original, and also to format the numbers as you expect. let res = input.map( ([a, b, c, d]) => [a, b.toFixed(2), c.toFixed(2), d.toFixed(2)] ); // Now we sort the new array using a more-like mathematical expression. res.sort((x, y) => { let n = x[1] - y[1], m = x[2] - y[2]; return n + m * (n === 0); }); console.log(res); 

Just chain the wanted order by taking the delta of each column. 只需通过获取每列的增量来链接所需的订单即可。

 var array = [["2015-08-09", 1.2, 1.2], ["2015-08-10", 1.2, 1.21], ["2015-08-11", 1.2, 0.99], ["2015-10-29", 1.2, 1.12], ["2015-09-10", 1.21, 1.19]]; array.sort((a, b) => a[1] - b[1] || a[2] - b[2]); console.log(array.map(a => a.join(' '))); 

Actually your array is 2-dimensional. 实际上,您的数组是二维的。 You can sort it the same way as 1-dimensional array: 您可以使用与一维数组相同的方式对其进行排序:

 const data = [ [ "2015-08-09", 1.2, 1.2], [ "2015-08-10", 1.2, 1.21], [ "2015-08-11", 1.2, 0.99], [ "2015-10-29", 1.2, 1.12] , [ "2015-09-10", 1.21, 1.19] ] function sort(a, b) { const col1 = new Date(a[0]) - new Date(b[0]); if (col1 === 0) { return a[1] - b[1]; } return col1; } console.log(data.sort(sort)) 

In above example we sort first by the first field in inner array. 在上面的示例中,我们首先按内部数组中的第一个字段进行排序。 If values are equal - then we compare the second field of inner array. 如果值相等-那么我们比较内部数组的第二个字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM