javascript - Sort Tree Structure of Objects by Class Property -


i have tree structure of objects (javascript/typescript), derived of same class (t). each object may or may not contain children of same object (list). therefore children may or may not contain own children (list), , on...

i'm needing sort each of these "levels" of nodes specific property of object.

currently i'm handling each parent , children:

                    nodes.sort((a, b) => {                         return a.id- b.id;                     });                                             nodes.foreach(function (node) {                         node.children.sort((a, b) => {                             return a.id- b.id;                         });                     }); 

however, i'm needing method sort children of children, children of children, , etc. imagine involve recursion, i'm not sure best way of handling in terms of efficiency.

a) sort nodes.

b) go through each node, if node has children, repeat step children.

function sortnodesandchildren(nodes) {     nodes.sort();     nodes.foreach(function (node) {         if (nodehaschildren(node)) {             sortnodesandchildren(node.children);         }     } } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -