通过将最后一个值与下一个索引中的第一个值进行匹配来对二维数组进行排序
Sort 2D array by matching the last values with the first one in the next index
基本上,我有这个数组
array = [[1,0],[2,1],[0,3],[3,2]]
有什么快速的方法可以将数组转换成这样
array = [[0,3],[3,2],[2,1],[1,0]]
我想要的是新数组的第一个元素在嵌套数组的第一个位置始终包含 0。这很简单,因为 sort()
函数正是这样做的;困难的部分是像上面那样对新数组进行排序。
用最简单的术语来说,我希望嵌套数组是“连接的”:看看第一个嵌套数组的 3
如何与右边的另一个 3
相匹配等等上。
请随时留下任何评论,以便我尝试更好地解释问题。
对于您给出的示例,最简单的解决方案是按每个内部数组的第二个元素对二维数组进行排序,如下所示:
let array = [[1,0],[2,1],[0,3],[3,2]];
array.sort((a, b) => b[1] - a[1]);
这样你就可以根据内部数组中的元素使用sort方法对数组进行排序。
let array = [[1,0],[2,1],[0,3],[3,2]];
array.sort((a, b) => b[1] - a[1]);
console.log(array);
您可以将一个对象作为参考,并通过链接项重建数组。
const
getItems = (reference, value) => {
const a = reference[value];
return a ? [a, ...(a[1] === 0 ? [] : getItems(reference, a[1]))] : [];
},
array = [[1, 0], [2, 1], [0, 3], [3, 2]],
reference = array.reduce((r, a) => (r[a[0]] = a, r), {}),
result = getItems(reference, 0);
console.log(result);
看这个方法:
const array = [[1,0],[2,1],[0,3],[3,2]];
const result = [...array];
// Put the subarray with the zero element first
// You can use the sort() function but I guess this method
// performs better in terms of time
for (let i = 0; i < result.length; ++i) {
if (result[i][0] === 0) {
result.unshift(...result.splice(i, 1));
break;
}
}
// Now reorder the array so that the last index of a subarray
// matches with the first index of the other subarray
for (let i = 0; i < result.length; ++i) {
for (let j = i + 1; j < result.length; ++j) {
if (result[i][1] === result[j][0]) {
// recollocate the subarray so that it matches the way we want
result.splice(i + 1, 0, ...result.splice(j, 1));
break;
}
}
}
console.log(result)
基本上,我有这个数组
array = [[1,0],[2,1],[0,3],[3,2]]
有什么快速的方法可以将数组转换成这样
array = [[0,3],[3,2],[2,1],[1,0]]
我想要的是新数组的第一个元素在嵌套数组的第一个位置始终包含 0。这很简单,因为 sort()
函数正是这样做的;困难的部分是像上面那样对新数组进行排序。
用最简单的术语来说,我希望嵌套数组是“连接的”:看看第一个嵌套数组的 3
如何与右边的另一个 3
相匹配等等上。
请随时留下任何评论,以便我尝试更好地解释问题。
对于您给出的示例,最简单的解决方案是按每个内部数组的第二个元素对二维数组进行排序,如下所示:
let array = [[1,0],[2,1],[0,3],[3,2]];
array.sort((a, b) => b[1] - a[1]);
这样你就可以根据内部数组中的元素使用sort方法对数组进行排序。
let array = [[1,0],[2,1],[0,3],[3,2]];
array.sort((a, b) => b[1] - a[1]);
console.log(array);
您可以将一个对象作为参考,并通过链接项重建数组。
const
getItems = (reference, value) => {
const a = reference[value];
return a ? [a, ...(a[1] === 0 ? [] : getItems(reference, a[1]))] : [];
},
array = [[1, 0], [2, 1], [0, 3], [3, 2]],
reference = array.reduce((r, a) => (r[a[0]] = a, r), {}),
result = getItems(reference, 0);
console.log(result);
看这个方法:
const array = [[1,0],[2,1],[0,3],[3,2]];
const result = [...array];
// Put the subarray with the zero element first
// You can use the sort() function but I guess this method
// performs better in terms of time
for (let i = 0; i < result.length; ++i) {
if (result[i][0] === 0) {
result.unshift(...result.splice(i, 1));
break;
}
}
// Now reorder the array so that the last index of a subarray
// matches with the first index of the other subarray
for (let i = 0; i < result.length; ++i) {
for (let j = i + 1; j < result.length; ++j) {
if (result[i][1] === result[j][0]) {
// recollocate the subarray so that it matches the way we want
result.splice(i + 1, 0, ...result.splice(j, 1));
break;
}
}
}
console.log(result)