diff --git a/src/Collections/Collection.php b/src/Collections/Collection.php
index f7fec10..cf58f32 100644
--- a/src/Collections/Collection.php
+++ b/src/Collections/Collection.php
@@ -136,14 +136,14 @@ class Collection extends ImmutableCollection implements ICollection {
* @inheritDoc
*/
public function sortSelf (Closure|IComparator|null $sorter = null): static {
- uasort($this->elements, $sorter);
+ uasort($this->elements, self::_normalizeSorter($sorter));
return $this;
}
/**
* @inheritDoc
*/
public function sortSelfByKey (Closure|IComparator $sorter): static {
- uksort($this->elements, $sorter);
+ uksort($this->elements, self::_normalizeSorter($sorter));
return $this;
}
}
\ No newline at end of file
diff --git a/src/Collections/ImmutableCollection.php b/src/Collections/ImmutableCollection.php
index 353a155..4dc51b9 100644
--- a/src/Collections/ImmutableCollection.php
+++ b/src/Collections/ImmutableCollection.php
@@ -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}.
+ *
Return :
+ *
< 0 if value1 is before value2
+ *
= 0 if value1 equals value2
+ *
> 0 if value1 is after value2
+ *
+ * @return Closure(TValue, TValue):int The normalized sorter method
+ *
Return :
+ *
< 0 if value1 is before value2
+ *
= 0 if value1 equals value2
+ *
> 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
*/
diff --git a/tests/test_class.php b/tests/test_class.php
index 837f4db..db96292 100644
--- a/tests/test_class.php
+++ b/tests/test_class.php
@@ -65,12 +65,15 @@ function getMeasures (): Collection {
return $measures;
}
+$measures = getMeasures();
+$measures->sortSelf();
+
echo 'Measures :' . PHP_EOL;
-foreach (getMeasures()->sort() as $no => $measure) {
+foreach ($measures as $no => $measure) {
echo "\t#" . $no . ' = ' . $measure->asString() . 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;
}
\ No newline at end of file