我有一个按以下顺序排列的数组
[
{
"source": 0,
"target": 1,
"value": 22
},
{
"source": 1,
"target": 2,
"value": 22
},
{
"source": 0,
"target": 1,
"value": 19
},
{
"source": 1,
"target": 3,
"value": 19
},
{
"source": 0,
"target": 1,
"value": 12
},
{
"source": 1,
"target": 4,
"value": 12
},
{
"source": 0,
"target": 1,
"value": 3.230000000000002
},
{
"source": 1,
"target": 5,
"value": 3.230000000000002
},
{
"source": 0,
"target": 6,
"value": 109
},
{
"source": 6,
"target": 7,
"value": 109
},
{
"source": 0,
"target": 6,
"value": 25
},
{
"source": 6,
"target": 8,
"value": 25
},
{
"source": 0,
"target": 6,
"value": 20
},
{
"source": 6,
"target": 3,
"value": 20
},
{
"source": 0,
"target": 6,
"value": 17
},
{
"source": 6,
"target": 9,
"value": 17
},
{
"source": 0,
"target": 6,
"value": 13
},
{
"source": 6,
"target": 10,
"value": 13
},
{
"source": 0,
"target": 6,
"value": 11
},
{
"source": 6,
"target": 11,
"value": 11
},
{
"source": 0,
"target": 6,
"value": 7
},
{
"source": 6,
"target": 12,
"value": 7
},
{
"source": 0,
"target": 6,
"value": 7
},
{
"source": 6,
"target": 13,
"value": 7
},
{
"source": 0,
"target": 6,
"value": 6
},
{
"source": 6,
"target": 14,
"value": 6
},
{
"source": 0,
"target": 6,
"value": 4
},
{
"source": 6,
"target": 15,
"value": 4
},
{
"source": 0,
"target": 6,
"value": 2
},
{
"source": 6,
"target": 4,
"value": 2
}
]
我需要按以下顺序对其进行排序
[
{
"source": 0,
"target": 1,
"value": 3.230000000000002
},
{
"source": 1,
"target": 5,
"value": 3.230000000000002
},
{
"source": 0,
"target": 1,
"value": 12
},
{
"source": 1,
"target": 4,
"value": 12
},
{
"source": 0,
"target": 1,
"value": 19
},
{
"source": 1,
"target": 3,
"value": 19
},
{
"source": 0,
"target": 1,
"value": 22
},
{
"source": 1,
"target": 2,
"value": 22
},
{
"source": 0,
"target": 6,
"value": 2
},
{
"source": 6,
"target": 4,
"value": 2
},
{
"source": 0,
"target": 6,
"value": 4
},
{
"source": 6,
"target": 15,
"value": 4
},
{
"source": 0,
"target": 6,
"value": 6
},
{
"source": 6,
"target": 14,
"value": 6
},
{
"source": 0,
"target": 6,
"value": 7
},
{
"source": 6,
"target": 12,
"value": 7
},
{
"source": 0,
"target": 6,
"value": 7
},
{
"source": 6,
"target": 13,
"value": 7
},
{
"source": 0,
"target": 6,
"value": 11
},
{
"source": 6,
"target": 11,
"value": 11
},
{
"source": 0,
"target": 6,
"value": 13
},
{
"source": 6,
"target": 10,
"value": 13
},
{
"source": 0,
"target": 6,
"value": 17
},
{
"source": 6,
"target": 9,
"value": 17
},
{
"source": 0,
"target": 6,
"value": 20
},
{
"source": 6,
"target": 3,
"value": 20
},
{
"source": 0,
"target": 6,
"value": 25
},
{
"source": 6,
"target": 8,
"value": 25
},
{
"source": 0,
"target": 6,
"value": 109
},
{
"source": 6,
"target": 7,
"value": 109
}
]
到目前为止,我已经在下面尝试过,首先基于 进行排序source,target and value
,现在我不知道如何在第二步中进行排序以获得所需的结果。任何帮助/建议将不胜感激。
var data = [{
"source": 0,
"target": 1,
"value": 22
},
{
"source": 1,
"target": 2,
"value": 22
},
{
"source": 0,
"target": 1,
"value": 19
},
{
"source": 1,
"target": 3,
"value": 19
},
{
"source": 0,
"target": 1,
"value": 12
},
{
"source": 1,
"target": 4,
"value": 12
},
{
"source": 0,
"target": 1,
"value": 3.230000000000002
},
{
"source": 1,
"target": 5,
"value": 3.230000000000002
},
{
"source": 0,
"target": 6,
"value": 109
},
{
"source": 6,
"target": 7,
"value": 109
},
{
"source": 0,
"target": 6,
"value": 25
},
{
"source": 6,
"target": 8,
"value": 25
},
{
"source": 0,
"target": 6,
"value": 20
},
{
"source": 6,
"target": 3,
"value": 20
},
{
"source": 0,
"target": 6,
"value": 17
},
{
"source": 6,
"target": 9,
"value": 17
},
{
"source": 0,
"target": 6,
"value": 13
},
{
"source": 6,
"target": 10,
"value": 13
},
{
"source": 0,
"target": 6,
"value": 11
},
{
"source": 6,
"target": 11,
"value": 11
},
{
"source": 0,
"target": 6,
"value": 7
},
{
"source": 6,
"target": 12,
"value": 7
},
{
"source": 0,
"target": 6,
"value": 7
},
{
"source": 6,
"target": 13,
"value": 7
},
{
"source": 0,
"target": 6,
"value": 6
},
{
"source": 6,
"target": 14,
"value": 6
},
{
"source": 0,
"target": 6,
"value": 4
},
{
"source": 6,
"target": 15,
"value": 4
},
{
"source": 0,
"target": 6,
"value": 2
},
{
"source": 6,
"target": 4,
"value": 2
}
];
data.sort(function(a, b) {
return a.source - b.source || a.target - b.target || a.value - b.value;
});
console.log(JSON.stringify(data));
更新 1
源和目标之间存在黑白关系。排序后的数组应按以下顺序排列
基于源、目标、值命名为“A”的最小元素
源 = 最小源
目标 = 最小目标值
= 最小值
所有谁是元素
源=元素的靶
目标=
值=元素A值
..................
下更小的元素命名基于源,目标,值“B”
的源=较小的源
目标 = 较小的目标值
= 较小的值
所有谁是元素
源= B元素靶
目标=
值=元件B值
...................
基于源,目标,值接着命名为“C”小的元件
的源= 小源
目标 = 小目标值
= 小值
所有谁是元素
源=元件C目标
靶=
值=元件C值
等等等等。
您可以通过查看以前的target and following
源代码来保留同一组,将它们组合在一起,按值排序并获得对象的平面数组。
var data = [{ source: 0, target: 1, value: 22 }, { source: 1, target: 2, value: 22 }, { source: 0, target: 1, value: 19 }, { source: 1, target: 3, value: 19 }, { source: 0, target: 1, value: 12 }, { source: 1, target: 4, value: 12 }, { source: 0, target: 1, value: 3.230000000000002 }, { source: 1, target: 5, value: 3.230000000000002 }, { source: 0, target: 6, value: 109 }, { source: 6, target: 7, value: 109 }, { source: 0, target: 6, value: 25 }, { source: 6, target: 8, value: 25 }, { source: 0, target: 6, value: 20 }, { source: 6, target: 3, value: 20 }, { source: 0, target: 6, value: 17 }, { source: 6, target: 9, value: 17 }, { source: 0, target: 6, value: 13 }, { source: 6, target: 10, value: 13 }, { source: 0, target: 6, value: 11 }, { source: 6, target: 11, value: 11 }, { source: 0, target: 6, value: 7 }, { source: 6, target: 12, value: 7 }, { source: 0, target: 6, value: 7 }, { source: 6, target: 13, value: 7 }, { source: 0, target: 6, value: 6 }, { source: 6, target: 14, value: 6 }, { source: 0, target: 6, value: 4 }, { source: 6, target: 15, value: 4 }, { source: 0, target: 6, value: 2 }, { source: 6, target: 4, value: 2 }],
result = data
.reduce((r, o) => {
let last = r[r.length - 1];
if (last && last[last.length - 1].target === o.source) last.push(o);
else r.push([o]);
return r;
}, [])
.sort(([a], [b]) => a.source - b.source || a.target - b.target || a.value - b.value)
.flat();
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句