FluidTYPO3\Flux\Service\FluxService::sortObjectsByProperty PHP Метод

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

public sortObjectsByProperty ( array $objects, string $sortBy, string $sortDirection = 'ASC' ) : array
$objects array
$sortBy string
$sortDirection string
Результат array
    public function sortObjectsByProperty(array $objects, $sortBy, $sortDirection = 'ASC')
    {
        $sorted = array();
        $sort = array();
        foreach ($objects as $index => $object) {
            $sortValue = ObjectAccess::getPropertyPath($object, $sortBy);
            $sort[$index] = $sortValue;
        }
        if ('ASC' === strtoupper($sortDirection)) {
            asort($sort);
        } else {
            arsort($sort);
        }
        $hasStringIndex = FALSE;
        foreach ($sort as $index => $value) {
            $sorted[$index] = $objects[$index];
            if (TRUE === is_string($index)) {
                $hasStringIndex = TRUE;
            }
        }
        if (FALSE === $hasStringIndex) {
            // reset out-of-sequence indices if provided indices contain no strings
            $sorted = array_values($sorted);
        }
        return $sorted;
    }

Usage Example

Пример #1
0
 /**
  * @test
  * @dataProvider getSortObjectsTestValues
  * @param array $input
  * @param string $sortBy
  * @param string $direction
  * @param array $expectedOutput
  */
 public function testSortObjectsByProperty($input, $sortBy, $direction, $expectedOutput)
 {
     $service = new FluxService();
     $sorted = $service->sortObjectsByProperty($input, $sortBy, $direction);
     $this->assertEquals($expectedOutput, $sorted);
 }