Class SortIterator
An iterator that will return the passed items in order. The order is given by the value returned in a callback function that maps each of the elements.
Example:
$items = [$user1, $user2, $user3]; $sorted = new SortIterator($items, function ($user) { return $user->age; }); // output all user name order by their age in descending order foreach ($sorted as $user) { echo $user->name; }
This iterator does not preserve the keys passed in the original elements.
- IteratorIterator implements Iterator, Traversable, OuterIterator
- Cake\Collection\Collection implements Cake\Collection\CollectionInterface, Serializable uses Cake\Collection\CollectionTrait
- Cake\Collection\Iterator\SortIterator
Method Summary
-
__construct() public
Wraps this iterator around the passed items so when iterated they are returned in order.
-
unwrap() public
Method Detail
__construct() public ¶
__construct( array|Traversable $items , callable|string $callback , integer $dir \SORT_DESC , integer $type \SORT_NUMERIC )
Wraps this iterator around the passed items so when iterated they are returned in order.
The callback will receive as first argument each of the elements in $items, the value returned in the callback will be used as the value for sorting such element. Please note that the callback function could be called more than once per element.
Parameters
- array|Traversable $items
- The values to sort
- callable|string $callback
A function used to return the actual value to be compared. It can also be a string representing the path to use to fetch a column or property in each element
- integer $dir optional \SORT_DESC
- either SORT_DESC or SORT_ASC
- integer $type optional \SORT_NUMERIC
the type of comparison to perform, either SORT_STRING SORT_NUMERIC or SORT_NATURAL
Throws
If passed incorrect type for items.
Overrides
Methods inherited from Cake\Collection\Collection
__debugInfo() public ¶
__debugInfo( )
Returns an array that can be used to describe the internal state of this object.
Returns
count() public ¶
count( )
Returns the amount of elements in the collection.
Returns
Implementation of
countKeys() public ¶
countKeys( )
Returns the number of unique keys in this iterator. This is, the number of
elements the collection will contain after calling toArray()
Returns
Implementation of
serialize() public ¶
serialize( )
Returns a string representation of this object that can be used to reconstruct it
Returns
Implementation of
Serializable::serialize()
unserialize() public ¶
unserialize( string $collection )
Unserializes the passed string and rebuilds the Collection instance
Parameters
- string $collection
- The serialized collection
Implementation of
Serializable::unserialize()
Methods used from Cake\Collection\CollectionTrait
cartesianProduct() public ¶
cartesianProduct( callable $operation null , callable $filter null )
Parameters
- callable $operation optional null
- Operation
- callable $filter optional null
- Filter
Returns
Throws
optimizeUnwrap() protected ¶
optimizeUnwrap( )
Unwraps this iterator and returns the simplest traversable that can be used for getting the data out
Returns
Magic methods inherited from Cake\Collection\CollectionInterface
cartesianProduct()
|