Sorting interdependant objects (in-line process) with javascript / node

I've been breaking my head over this for quite some time and hope to get some support here. I have an array containing multiple objects.

Every object represents a production process comprised of subprocesses made up of inputs and outputs. These processes run in line. Meaning, that the output of one process will become the input for another process. However, I have no idea in which order.

I am trying to figure out the sequence the main processes need to be run in. Our system can't tell me and now I have 5000interdependant processes that need sorting.

Here an example of how the array is formatted. As you can see Inputs have an origin flag. At least we know whether an input is taken from a warehouse or is produced somewhere upstream. If I were to sort this manually the order would be Process3, then Process 2, then Process 1. Process 1 requires the material with the id="ID3" as input, which is produced in process 2 (under subprocess 2). While Process 2 requires the material "ID5" which is produced in process 3. Anybody have an idea how to solve this nightmare?

Thank you sooo much!!!

[
  {
    processID: "1",
    subprocesses: [
      {
        subprocessID: "1",
        outputs: [
          {
            id: "ID1",
            name: "alpha",
            ratio: "1",
          },
        ],
        inputs: [
          {
            type: "rawMaterial",
            id: "ID2",
            ratio: "0.7623",
          },
          {
            type: "processOutput",
            id: "ID3",
            ratio: "0.6552",
          },
        ],
      },
    ],
  },
  {
    processID: "2",
    subprocesses: [
      {
        subprocessID: "1",
        outputs: [
          {
            id: "ID22",
            name: "beta)",
            ratio: "1",
          },
        ],
        inputs: [
          {
            type: "processOutput",
            id: "ID5",
            ratio: "0.0034",
          },
        ],
      },
      {
        subprocessID: "2",
        outputs: [
          {
            id: "ID3",
            name: "gamma",
            ratio: "1",
          },
        ],
        inputs: [
          {
            type: "rawMaterial",
            id: "ID10",
            ratio: "0.0034",
          },
        ],
      },
    ],
  },
  {
    processID: "3",
    subprocesses: [
      {
        subprocessID: "1",
        outputs: [
          {
            id: "ID5",
            name: "omega",
            ratio: "1",
          },
        ],
        inputs: [
          {
            type: "rawMaterial",
            id: "ID111",
            ratio: "0.3018",
          },
        ],
      },
    ],
  },
]


Read more here: https://stackoverflow.com/questions/67921113/sorting-interdependant-objects-in-line-process-with-javascript-node

Content Attribution

This content was originally published by Pat Binge at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: