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 = lmbTreeHelper::sort(new lmbCollection($raw_tree_array), array('sort1' => 'DESC', 'sort2' => 'ASC'));
     $to_check = array();
     foreach ($sorted as $record) {
         $to_check[] = $record->export();
     }
     $this->assertEqual($to_check, $expected_tree_array);
 }
 function rewind()
 {
     parent::rewind();
     if ($this->iterator->valid()) {
         $nested_array = array();
         $iterator = lmbTreeHelper::sort($this->iterator, $this->order_pairs, $this->node_field, $this->parent_field);
     } else {
         $iterator = new lmbCollection();
     }
     $this->iterator = $iterator;
     return $this->iterator->rewind();
 }
Example #3
0
 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 = lmbArrayHelper::sortArray($children, $sort_params);
     if (!$sorted_tree_array) {
         $sorted_tree_array = $children;
     } else {
         $ids = lmbArrayHelper::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++) {
         lmbTreeHelper::_doSort($tree_array, $sorted_tree_array, $sort_params, $children[$i][$id_hash], $id_hash, $parent_hash);
     }
 }