如何根据对象键对数组 ob 对象进行排序?

厄齐尔

我有一个按以下顺序排列的数组

[
  {
    "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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何根据对象属性对数组中的对象键进行排序?

根据数组对象对数组进行排序

如何根据其动态对象的值对数组进行排序

如何根据价格对数组中的对象进行排序

从对象对数组进行排序

如何根据键对对象的javascript数组进行排序

根据优先级数组对数组对象进行排序

根据对象内部的价格对数组进行排序

根据包含时间戳的对象值对数组进行排序

Vue:根据对象顺序对数组进行排序

Angular 2:根据嵌套对象的属性对数组进行排序

根据对象中字段的存在对数组进行排序

根据嵌套对象值对数组进行排序

根据对象的属性之一对数组进行排序

如何根据数组中的一个对象对数组进行数值排序?

如何首先根据键对数组进行排序?

根据数组的键/值对对象数组进行排序

如何对数组内的不同对象数组进行排序

如何访问数组中对象的属性并对数组进行排序?

如何根据另一个对象对数组进行排序?

如何通过那些对象键对包含对象的数组进行排序

在JavaScript中对数组对象进行排序

从 AZ 对数组内的对象进行排序

按对象字段对数组进行排序

如何根据对象的属性对对象数组进行排序

如何根据对象的值对对象数组进行分组排序?

如何根据键对对象数组进行排序并设置应在顺序更改时递增的添加键?

根据特定键递归对对象数组进行排序:Javascript

根据键的存在和值对对象数组进行排序