function testSortComplexRs() { $raw_tree_array = array( array('id' => 1, 'parent_id' => 0, 'sort1' => 'bill', 'sort2' => 0), array('id' => 2, 'parent_id' => 1, 'sort1' => 'body', 'sort2' => 1), array('id' => 3, 'parent_id' => 2, 'sort1' => 'merfy', 'sort2' => 0), array('id' => 4, 'parent_id' => 2, 'sort1' => 'eddy', 'sort2' => 1), array('id' => 5, 'parent_id' => 1, 'sort1' => 'body', 'sort2' => 0), array('id' => 6, 'parent_id' => 0, 'sort1' => 'alfred', 'sort2' => 1), array('id' => 7, 'parent_id' => 6, 'sort1' => 'tom', 'sort2' => 0), array('id' => 8, 'parent_id' => 0, 'sort1' => 'cunny', 'sort2' => 4), ); $expected_tree_array = array( array('id' => 8, 'parent_id' => 0, 'sort1' => 'cunny', 'sort2' => 4), array('id' => 1, 'parent_id' => 0, 'sort1' => 'bill', 'sort2' => 0), array('id' => 5, 'parent_id' => 1, 'sort1' => 'body', 'sort2' => 0), array('id' => 2, 'parent_id' => 1, 'sort1' => 'body', 'sort2' => 1), array('id' => 3, 'parent_id' => 2, 'sort1' => 'merfy', 'sort2' => 0), array('id' => 4, 'parent_id' => 2, 'sort1' => 'eddy', 'sort2' => 1), array('id' => 6, 'parent_id' => 0, 'sort1' => 'alfred', 'sort2' => 1), array('id' => 7, 'parent_id' => 6, 'sort1' => 'tom', 'sort2' => 0), ); $sorted = TreeRsProcessor :: sort(new ArrayDataSet($raw_tree_array), array('sort1' => 'DESC', 'sort2' => 'ASC')); $this->assertEqual( $sorted, new ArrayDataSet($expected_tree_array) ); }
function _doSort($tree_array, &$sorted_tree_array, $sort_params, $parent_id, $id_hash, $parent_hash) { $children = array(); foreach($tree_array as $index => $item) { if($item[$parent_hash] == $parent_id) { $children[] = $item; unset($tree_array[$index]); } } if(!($count = sizeof($children))) return; $children = ComplexArray :: sortArray($children, $sort_params); if(!$sorted_tree_array) { $sorted_tree_array = $children; } else { $ids = ComplexArray :: getColumnValues($id_hash, $sorted_tree_array); $offset = array_search($parent_id, $ids) + 1; array_splice($sorted_tree_array, $offset, 0, $children); } for($i=0; $i < $count; $i++) { TreeRsProcessor :: _doSort($tree_array, $sorted_tree_array, $sort_params, $children[$i][$id_hash], $id_hash, $parent_hash); } }