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>'));

更多相关文章

  1. JS将字符串转换为数组
  2. 将textarea值附加到现有数组javascript
  3. ECMAScript6(6):数组的扩展
  4. 希望日期开始结束在一个数组中的while循环中为一个房间ID
  5. 如何使用客户端Javascript数组并通过节点发布。将js API插入Mong
  6. 在量角器中检索子元素的数组
  7. 从特定条件下存储在localStorage中的数组中删除对象?
  8. Python 3.4:试图让这个模块对这个2d数组中的销售总数求和。不工作
  9. Python:2D Numpy数组(矩阵) - 查找负数之和(行)

随机推荐

  1. Android_android studio使用 9patch常见
  2. [转]Android事件处理
  3. TextView设置android:ellipsize="marquee
  4. android 底部菜单
  5. android之【TableLayout布局】
  6. cocos2dx android mac
  7. Android 文件下载 downloadManager
  8. android 作业1相对布局(拼图)
  9. android之实现SeekBar拖动组件
  10. EditText设置不自动弹出键盘