[英]How to sort an array ob objects on the basis of object keys?
I have an array in following order我有一个按以下顺序排列的数组
[
{
"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
}
]
I need to sort it in following order我需要按以下顺序对其进行排序
[
{
"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
}
]
So far I have tried below, first sort on base of source,target and value
, now I don't know how to sort in second step to get desired out.到目前为止,我已经在下面尝试过,首先根据 s source,target and value
进行排序,现在我不知道如何在第二步中进行排序以获得所需的结果。 Any help/suggestion will be appreciated.任何帮助/建议将不胜感激。
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));
UpDate 1更新日期 1
There is a relationship b/w source and target. b/w 源和目标之间存在关系。 Sorted array should be in the following sequence排序后的数组应按以下顺序
smallest element named "A" based on source, target, value基于源、目标、值的名为“A”的最小元素
source = smallest source来源 = 最小来源
target = smallest target目标 = 最小目标
value = smallest value值 = 最小值
all the elements who's所有的元素
source = element A target源 = 元素 A 目标
target =目标=
Value = element A value值 = 元素 A 值
.................. .....................
Next smaller element named "B" based on source, target, value下一个基于源、目标、值的名为“B”的较小元素
source = smaller source来源 = 较小的来源
target = smaller target目标=较小的目标
value = smaller value值 = 较小的值
all the elements who's所有的元素
source = element B target源 = 元素 B 目标
target =目标=
Value = element B value值 = 元素 B 值
................... .....................
Next small element named "C" based on source, target, value下一个基于源、目标、值的名为“C”的小元素
source = small source来源 = 小来源
target = small target目标=小目标
value = small value价值=小价值
all the elements who's所有的元素
source = element C target源 = 元素 C 目标
target =目标=
Value = element C value值 = 元素 C 值
so on and so forth.等等等等。
You could keep the same group by looking at previous target and following
source``, group them together, sort by value and get a flat array of objects.您可以通过查看前一个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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.