*/ class Measure implements IComparable { private int $value; private string $unit; public function __construct (int $value, string $unit = 's') { $this->value = $value; $this->unit = $unit; } public function getValue (): int { return $this->value; } public function asString (): string { return $this->value . $this->unit; } /** * @inheritDoc */ public function compareTo ($other): int { return $this->value <=> $other->value; } } /** * Class MeasureReverseComparator * * @implements IComparator */ class MeasureReverseComparator implements IComparator { /** * @inheritDoc */ public function compare ($object1, $object2): int { return -($object1->getValue() <=> $object2->getValue()); } } /** * @return Collection */ function getMeasures (): Collection { $mesures_avant = new Collection(); $mesures_avant->add(new Measure(38), new Measure(29)); $measures = new Collection(); $measures->add(new Measure(27), new Measure(34)); $measures->prependCollection($mesures_avant); return $measures; } echo 'Measures :' . PHP_EOL; foreach (getMeasures()->sort() as $no => $measure) { echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL; } echo 'Measures (reversed) :' . PHP_EOL; foreach (getMeasures()->sort(new MeasureReverseComparator()) as $no => $measure) { echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL; }