Don't check for empty group nodes, we still have field nodes at sub level.
All field nodes at level 0 has level 0 by default, no need to assign again.
Use uasort to reserve the keys while sorting array by values.