function AbstractCollection::sort
Return value
CollectionInterface<T>
Throws
InvalidPropertyOrMethod if the $propertyOrMethod does not exist on the elements in this collection.
UnsupportedOperationException if unable to call sort() on this collection.
Overrides CollectionInterface::sort
File
-
vendor/
ramsey/ collection/ src/ AbstractCollection.php, line 162
Class
- AbstractCollection
- This class provides a basic implementation of `CollectionInterface`, to minimize the effort required to implement this interface
Namespace
Ramsey\CollectionCode
public function sort(?string $propertyOrMethod = null, Sort $order = Sort::Ascending) : CollectionInterface {
$collection = clone $this;
usort($collection->data, function (mixed $a, mixed $b) use ($propertyOrMethod, $order) : int {
/** @var mixed $aValue */
$aValue = $this->extractValue($a, $propertyOrMethod);
/** @var mixed $bValue */
$bValue = $this->extractValue($b, $propertyOrMethod);
return ($aValue <=> $bValue) * ($order === Sort::Descending ? -1 : 1);
});
return $collection;
}