-
-
Notifications
You must be signed in to change notification settings - Fork 13
IEnumerable.max() method
Marcel Kloubert edited this page Sep 30, 2015
·
3 revisions
Gets the maximum value of that sequence (s. Max()).
public function max([ mixed $defValue = null
[, comparer $comparer = null ]]) : mixed;
Name | Type | Description |
---|---|---|
$defValue | mixed | [OPTIONAL] The value to return if sequence is empty. |
$comparer | [[comparer | Comparer]] |
Is returned if sequence is empty.
The custom comparer.
The maximum value of the sequence or the default value.
use \System\Linq;
$seq1 = Enumerable::fromValues(239, 5979, 1);
$seq2 = Enumerable::create();
// 5979
$a1 = $seq1->max('YS');
// 'YS', because no element found
$a2 = $seq2->max('YS');
use \System\Linq;
class MyComparableClass {
public $SortValue;
public function __construct($sortVal) {
$this->SortValue = $sortVal;
}
}
$seq1 = Enumerable::fromValues(new MyComparableClass(5979),
new MyComparableClass(1),
new MyComparableClass(239));
$seq2 = Enumerable::create();
$comparer = '($x, $y) => $x->SortValue - $y->SortValue';
// instance of MyComparableClass
// with $a1->SortValue = 5979
$a1 = $seq1->max('PZ', $comparer);
// 'PZ', because no element found
$a2 = $seq2->max('PZ', $comparer);
use \System\Linq;
class MyComparableClass {
public $SortValue;
public function __construct($sortVal) {
$this->SortValue = $sortVal;
}
}
$seq1 = Enumerable::fromValues(new MyComparableClass(5979),
new MyComparableClass(1),
new MyComparableClass(239));
$seq2 = Enumerable::create();
$comparer = function(MyComparableClass $x, MyComparableClass $y) {
return $x->SortValue - $y->SortValue;
};
// instance of MyComparableClass
// with $a1->SortValue = 5979
$a1 = $seq1->max('MK', $comparer);
// 'MK', because no element found
$a2 = $seq2->max('MK', $comparer);