Documentation

SortableInterface

A sortable object.

Table of Contents

Methods

compare()  : int
Compare this object to another.

Methods

compare()

Compare this object to another.

public compare(mixed $other[, string $mode = 'default' ]) : int

Ultimately this always ends up in usort/uasort, something like:

usort($items, fn($a, $b) => $a->compare($b));

The return type should be the same as the spaceship operator, that is:

  • -1 $this < $other - first
  • 0 $this == $other - same
  • 1 $this > $other - last

If not comparable it's best to return -1 to move the 'other' lower down the list.

Tip, combine comparisons like so:

return $this->group <=> $other->group ?: $this->name <=> $this->name;
Parameters
$other : mixed
$mode : string = 'default'
Return values
int
On this page

Search results