code: def sort_nodes(self):
          
  
          node_map = {id(node): i for i, node in enumerate(self.nodes)}
          node_deps = {}
  
          for node, inputs, _outputs in self.iterate_bfs():
              node_index = node_map[id(node)]
  
              deps = {node_map[id(x)] for x in inputs}
              node_deps[node_index] = deps
  
          # Now that we have our dependency tree properly built, topologically
          # sort the nodes and reorder them.
          node_order = toposort_flatten(node_deps)
          self.nodes = [self.nodes[x] for x in node_order]
  
          #Check root nodes all topographically sorted to the beginning
          for root in self.roots:
              if root not in self.nodes[0:len(self.roots)]:
                  raise NodeConnectionError("Inputs not sorted in the beginning", node=str(root), node_position=self.nodes.index(root))
docs: Topologically sort all of our nodes.
  
          Topologically sorting our nodes makes nodes that are inputs to other
          nodes come first in the list of nodes.  This is important to do before
          programming a sensorgraph into an embedded device whose engine assumes
          a topologically sorted graph.
  
          The sorting is done in place on self.nodes