-
-
Notifications
You must be signed in to change notification settings - Fork 13
IEnumerable.orderBy() method
Marcel Kloubert edited this page Jun 29, 2021
·
11 revisions
Sorts the sequence in ascending order (s. OrderBy()).
public function orderBy(callable $selector
[, comparer $comparer = null ]) : IEnumerable;
Name | Type | Description |
---|---|---|
$selector | callable | The selector that provides the sort value. |
$comparer | comparer | [OPTIONAL] The custom sort algorithm. |
Provides the sort value.
function (mixed $item, IIndexedItemContext $ctx) : mixed;
The current (original) item.
The current item context.
The sort algorithm / comparer.
The new ordered sequence.
$seq = Enumerable::fromValues(2, 3, 1);
$seq = Enumerable::create(['b' => 2, 'c' => 3, 'a' => 1]);
foreach ($seq->orderBy('$x => $x') as $key => $item) {
// $key => 'a'; $item => 1
// $key => 'b'; $item => 2
// $key => 'c'; $item => 3
}
$seq = Enumerable::fromValues(2, 3, 1);
$seq = Enumerable::create(['b' => 2, 'c' => 3, 'a' => 1]);
foreach ($seq->orderBy(function($x) {
return $x;
}) as $key => $item) {
// $key => 'a'; $item => 1
// $key => 'b'; $item => 2
// $key => 'c'; $item => 3
}
$seq = Enumerable::create(['b' => 2, 'c' => 3, 'a' => 1]);
foreach ($seq->orderBy('$x => $x',
'($x, $y) => strcmp($y, $x)') as $key => $item) {
// $key => 'c'; $item => 3
// $key => 'b'; $item => 2
// $key => 'a'; $item => 1
}
$seq = Enumerable::create(['b' => 2, 'c' => 3, 'a' => 1]);
foreach ($seq->orderBy(true, // indicates to use the item itself
// as sort value
function ($x, $y) {
return strcmp($y, $x);
}) as $key => $item) {
// $key => 'c'; $item => 3
// $key => 'b'; $item => 2
// $key => 'a'; $item => 1
}