对多维数组中的列进行排序
16lz
2021-01-22
I want to sort a multidimensional array using the first row, I want that the second and the third row get the same sort order of the first row
我想用第一行对多维数组排序,我想让第二行和第三行得到第一行相同的排序顺序
Here is an example of array :
下面是数组的一个例子:
var arr =
[
[1,3,5,6,0,2],
[1,2,7,4,0,6],
[1,2,3,4,5,6]
]
the result that I want =
我想要的结果=。
[
[0,1,2,3,5,6],
[0,1,6,2,7,4],
[5,1,6,2,3,4]
]
I tried the following function but it doesnt work as I want
我尝试了下面的函数,但它不能按我想的那样工作
arr = arr.sort(function(a,b) {
return a[0] > b[0];
});
What's wrong with my function ? Thank you
我的函数怎么了?谢谢你!
2 个解决方案
#1
4
I think I understand what you're looking for:
我想我明白你在找什么:
a = [
[1,3,5,6,0,2],
[1,2,7,4,0,6],
[1,2,3,4,5,6]
]
transpose = m => m[0].map((_, c) => m.map(r => r[c]));
z = transpose(transpose(a).sort((x, y) => x[0] - y[0]))
z.map(r =>
document.write('<pre>'+JSON.stringify(r) + '</pre>'));
更多相关文章
- JS将字符串转换为数组
- 将textarea值附加到现有数组javascript
- ECMAScript6(6):数组的扩展
- 希望日期开始结束在一个数组中的while循环中为一个房间ID
- 如何使用客户端Javascript数组并通过节点发布。将js API插入Mong
- 在量角器中检索子元素的数组
- 从特定条件下存储在localStorage中的数组中删除对象?
- Python 3.4:试图让这个模块对这个2d数组中的销售总数求和。不工作
- Python:2D Numpy数组(矩阵) - 查找负数之和(行)