Sort nodes and correctly print navigation tree HTML