There are many solutions for building a tree array from a flat array based on numeric reference IDs, but I couldn\'t find any solution for creating a tree array where sorting is