Centralize et fix sorter method normalizer

master 3.3.0
Julien Rosset 2 years ago
parent 1f7783090a
commit 2d71d2a4a0

@ -136,14 +136,14 @@ class Collection extends ImmutableCollection implements ICollection {
* @inheritDoc * @inheritDoc
*/ */
public function sortSelf (Closure|IComparator|null $sorter = null): static { public function sortSelf (Closure|IComparator|null $sorter = null): static {
uasort($this->elements, $sorter); uasort($this->elements, self::_normalizeSorter($sorter));
return $this; return $this;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function sortSelfByKey (Closure|IComparator $sorter): static { public function sortSelfByKey (Closure|IComparator $sorter): static {
uksort($this->elements, $sorter); uksort($this->elements, self::_normalizeSorter($sorter));
return $this; return $this;
} }
} }

@ -286,19 +286,8 @@ class ImmutableCollection implements IImmutableCollection {
* @inheritDoc * @inheritDoc
*/ */
public function sort (Closure|IComparator|null $sorter = null): static { 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(); $elements = $this->toArray();
uasort($elements, $sorter); uasort($elements, self::_normalizeSorter($sorter));
return new static($elements); return new static($elements);
} }
@ -306,18 +295,43 @@ class ImmutableCollection implements IImmutableCollection {
* @inheritDoc * @inheritDoc
*/ */
public function sortByKey (Closure|IComparator $sorter): static { 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(); $elements = $this->toArray();
uksort($elements, $sorter); uksort($elements, self::_normalizeSorter($sorter));
return new static($elements); 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>&lt; 0 if value1 is before value2
* <br>= 0 if value1 equals value2
* <br>&gt; 0 if value1 is after value2
*
* @return Closure(TValue, TValue):int The normalized sorter method
* <br>Return :
* <br>&lt; 0 if value1 is before value2
* <br>= 0 if value1 equals value2
* <br>&gt; 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 * @inheritDoc
*/ */

@ -65,12 +65,15 @@ function getMeasures (): Collection {
return $measures; return $measures;
} }
$measures = getMeasures();
$measures->sortSelf();
echo 'Measures :' . PHP_EOL; echo 'Measures :' . PHP_EOL;
foreach (getMeasures()->sort() as $no => $measure) { foreach ($measures as $no => $measure) {
echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL; echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL;
} }
echo 'Measures (reversed) :' . PHP_EOL; echo 'Measures (reversed) :' . PHP_EOL;
foreach (getMeasures()->sort(new MeasureReverseComparator()) as $no => $measure) { foreach ($measures->sort(new MeasureReverseComparator()) as $no => $measure) {
echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL; echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL;
} }
Loading…
Cancel
Save