I'm starting out with a flat JSON input as such (simple example):
[
{id:1, value:'child1', parentId:2, sortOrder:1},
{id:2, value:'root1', parentId:null, sortOrder:1},
{id:3, value:'root2', parentId:null, sortOrder:2},
{id:4, value:'child2', parentId:1, sortOrder:2},
{id:5, value:'root3', parentId:null, sortOrder:3},
{id:6, value:'child1', parentId:2, sortOrder:1},
{id:7, value:'root4', parentId:null, sortOrder:4}
];
The input can be of arbitrary depth and length, and needs to reformatted to a collection of nested arrays based on the parent-child relationships, as well as sorted in ascending order at each nesting level.
Either native JS or UnderscoreJS can be used to format the output.
The output would be formatted as a json structure with a basic form:
root1
child1
child2
root2
child1
child1
child2
child2
etc...
Where each element is a JSON object.
The nesting depth can be anything since the data is most likely coming from a db table with the above flat structure.
Any thoughts?