Yosymfony\Spress\Core\Support\ArrayWrapper::sort PHP Метод

sort() публичный Метод

Sorts the array (ascendant by default).
public sort ( string $key = null, callable $callback = null ) : array
$key string Element to sort using "dot" notation. You can to escape a dot in a key surrendering with brackets: "[.]"
$callback callable Callback should be a function with the following signature: ```php function($element1, $element2) { // returns -1, 0 or 1 } ```
Результат array
    public function sort($key = null, callable $callback = null)
    {
        $elements = is_null($key) === true ? $this->array : $this->get($key);
        $sortCallback = is_null($callback) === false ? $callback : function ($element1, $element2) {
            if ($element1 == $element2) {
                return 0;
            }
            return $element1 < $element2 ? -1 : 1;
        };
        uasort($elements, $sortCallback);
        return $elements;
    }

Usage Example

Пример #1
0
 public function testSortKey()
 {
     $a = new ArrayWrapper(['level1' => ['a' => '2015-11-01', 'b' => '2015-11-03', 'c' => '2015-11-02']]);
     $b = $a->sort('level1');
     $this->assertCount(3, $b);
     $this->assertEquals('2015-11-01', array_values($b)[0]);
     $this->assertEquals('2015-11-02', array_values($b)[1]);
     $this->assertEquals('2015-11-03', array_values($b)[2]);
 }