waughj/flat-to-hierarchy-sorter

Sorts flat list of nodes into hierarchy.

v0.1.0 2019-10-18 17:48 UTC

This package is auto-updated.

Last update: 2024-04-19 03:38:38 UTC


README

Sorts flat list of nodes with IDs and parent IDs into layered list of nodes with IDs and lists of children to make iteration easier.

Example

use WaughJ\FlatToHierarchySorter\FlatToHierarchySorter;
use WaughJ\FlatToHierarchySorter\HierarchicalNode;

$old_list =
[
    new HierarchicalNode( 1, 0 ),
    new HierarchicalNode( 2, 1 ),
    new HierarchicalNode( 3, 1 ),
    new HierarchicalNode( 4, 2 )
];
$sorted_list = FlatToHierarchySorter::sort( $old_list );

will return to equivalent of:

new HierarchicalNode
(
    1,
    0,
    [
        new HierarchicalNode( 2, 1, [ new HierarchicalNode( 4, 2 ) ] ),
        new HierarchicalNode( 3, 1 )
    ]
);

Changelog

0.1.0

  • Initial release