|
|
|
@ -286,19 +286,8 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function sort (Closure|IComparator|null $sorter = null): static {
|
|
|
|
|
if ($sorter === null) {
|
|
|
|
|
$sorter = function (IComparable $value1, IComparable $value2): int {
|
|
|
|
|
return $value1->compareTo($value2);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
elseif ($sorter instanceof IComparator) {
|
|
|
|
|
$sorter = function ($value1, $value2) use ($sorter): int {
|
|
|
|
|
return $sorter->compare($value1, $value2);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$elements = $this->toArray();
|
|
|
|
|
uasort($elements, $sorter);
|
|
|
|
|
uasort($elements, self::_normalizeSorter($sorter));
|
|
|
|
|
|
|
|
|
|
return new static($elements);
|
|
|
|
|
}
|
|
|
|
@ -306,18 +295,43 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function sortByKey (Closure|IComparator $sorter): static {
|
|
|
|
|
if ($sorter instanceof IComparator) {
|
|
|
|
|
$sorter = function ($value1, $value2) use ($sorter): int {
|
|
|
|
|
return $sorter->compare($value1, $value2);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$elements = $this->toArray();
|
|
|
|
|
uksort($elements, $sorter);
|
|
|
|
|
uksort($elements, self::_normalizeSorter($sorter));
|
|
|
|
|
|
|
|
|
|
return new static($elements);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Normalize a sorter method
|
|
|
|
|
*
|
|
|
|
|
* @param null|IComparator|Closure(TValue, TValue):int $sorter The sorting method ; Null if values are object implementing {@see IComparable}.
|
|
|
|
|
* <br>Return :
|
|
|
|
|
* <br>< 0 if value1 is before value2
|
|
|
|
|
* <br>= 0 if value1 equals value2
|
|
|
|
|
* <br>> 0 if value1 is after value2
|
|
|
|
|
*
|
|
|
|
|
* @return Closure(TValue, TValue):int The normalized sorter method
|
|
|
|
|
* <br>Return :
|
|
|
|
|
* <br>< 0 if value1 is before value2
|
|
|
|
|
* <br>= 0 if value1 equals value2
|
|
|
|
|
* <br>> 0 if value1 is after value2
|
|
|
|
|
*/
|
|
|
|
|
public static function _normalizeSorter (Closure|IComparator|null $sorter): Closure {
|
|
|
|
|
if ($sorter === null) {
|
|
|
|
|
return function (IComparable $value1, IComparable $value2): int {
|
|
|
|
|
return $value1->compareTo($value2);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
elseif ($sorter instanceof IComparator) {
|
|
|
|
|
return function ($value1, $value2) use ($sorter): int {
|
|
|
|
|
return $sorter->compare($value1, $value2);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return $sorter;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|