Todaymade\Daux\Tree\Directory::sort PHP Method

sort() public method

public sort ( )
    public function sort()
    {
        // Separate the values into buckets to sort them separately
        $buckets = ['index' => [], 'numeric' => [], 'normal' => [], 'down_numeric' => [], 'down' => []];
        foreach ($this->children as $key => $entry) {
            $name = $entry->getName();
            if ($name == 'index' || $name == '_index') {
                $buckets['index'][$key] = $entry;
                continue;
            }
            if ($name[0] == '-') {
                if (is_numeric($name[1])) {
                    $exploded = explode('_', $name);
                    $buckets['down_numeric'][abs(substr($exploded[0], 1))][$key] = $entry;
                    continue;
                }
                $buckets['down'][$key] = $entry;
                continue;
            }
            if (is_numeric($name[0])) {
                $exploded = explode('_', $name);
                $buckets['numeric'][abs($exploded[0])][$key] = $entry;
                continue;
            }
            $buckets['normal'][$key] = $entry;
        }
        $final = [];
        foreach ($buckets as $name => $bucket) {
            if ($name == 'numeric' || $name == 'down_numeric') {
                ksort($bucket);
                foreach ($bucket as $sub_bucket) {
                    $final = $this->sortBucket($sub_bucket, $final);
                }
            } else {
                $final = $this->sortBucket($bucket, $final);
            }
        }
        $this->children = $final;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @dataProvider providerSort
  */
 public function testSort($list, $expected)
 {
     shuffle($list);
     $directory = new Directory(new Root(new Config(), ''), 'dir');
     foreach ($list as $value) {
         $entry = new Content($directory, $value);
         $entry->setName($value);
     }
     $directory->sort();
     $final = [];
     foreach ($directory->getEntries() as $obj) {
         $final[] = $obj->getName();
     }
     $this->assertEquals($expected, $final);
 }
All Usage Examples Of Todaymade\Daux\Tree\Directory::sort