I need to write something that will take data that looks like this:
B
b
c
a
A
b
a
D
a
b
a
C
And sort it like this:
A
a
b
B
a
b
c
C
D
a
a
b
The data looks exactly how I’m presenting it above (except for the letters). It is a multi-lined string where the number of tabs determine the level of hierarchy in the tree.
I want to be able to sort each level of hierarchy on it’s own.
I have been having trouble coming up with a decent algorithm so I’m asking here.
I am doing this in PHP, however any pseudo-code approach would be greatly appreciated.
Also, I realize that I can first build a tree and then sort and output that tree, but I am trying to find a more elegant solution.
Thanks.
I actually solved this as I was in the process of asking, so I’ll answer my own question which may be helpful for somebody else on here. There are probably other good answers as well…
And here is the example usage: