public function sort(string $propertyOrMethod, string $order = self::SORT_ASC): CollectionInterface
    {
        if (!\in_array($order, [self::SORT_ASC, self::SORT_DESC], true)) {
            throw new InvalidSortOrderException('Invalid sort order given: ' . $order);
        }

        $collection = clone $this;

        \usort($collection->data, function ($a, $b) use ($propertyOrMethod, $order) {
            $aValue = $this->extractValue($a, $propertyOrMethod);
            $bValue = $this->extractValue($b, $propertyOrMethod);

            return ($aValue <=> $bValue) * ($order === self::SORT_DESC ? -1 : 1);
        });

        return $collection;
    }
public function sort(string $propertyOrMethod, string $order = self::SORT_ASC): CollectionInterface
    {
        if (!\in_array($order, [self::SORT_ASC, self::SORT_DESC], true)) {
            throw new InvalidSortOrderException('Invalid sort order given: ' . $order);
        }

        $collection = clone $this;

        \usort($collection->data, function ($a, $b) use ($propertyOrMethod, $order) {
            $aValue = $this->extractValue($a, $propertyOrMethod);
            $bValue = $this->extractValue($b, $propertyOrMethod);

            return ($aValue <=> $bValue) * ($order === self::SORT_DESC ? -1 : 1);
        });

        return $collection;
    }
public function sort ( string $ propertyOrMethod , string $ order = self :: SORT_ASC ) : CollectionInterface { if ( ! \ in_array ( $ order , [ self :: SORT_ASC , self :: SORT_DESC ] , true ) ) { throw new InvalidSortOrderException ( 'Invalid sort order given: ' . $ order ) ; } $ collection = clone $ this ; \ usort ( $ collection -> data , function ( $ a , $ b ) use ( $ propertyOrMethod , $ order ) { $ aValue = $ this -> extractValue ( $ a , $ propertyOrMethod ) ; $ bValue = $ this -> extractValue ( $ b , $ propertyOrMethod ) ; return ( $ aValue <=> $ bValue ) * ( $ order === self :: SORT_DESC ? - 1 : 1 ) ; } ) ; return $ collection ; }
Returns a sorted collection.

{@inheritdoc}

@param string $propertyOrMethod The property or method to sort by.
@param string $order The sort order for the resulting collection (one of
this interface's `SORT_*` constants).

@return self

@throws InvalidSortOrderException if neither "asc" nor "desc" was given
as the order.
@throws ValueExtractionException if property or method is not defined.
Returns a sorted collection .