The comparator function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
To obtain comparator one may use
Comparator
class (for instance Comparator::natural()
which yields ordering using comparison operators).
Example:
class Foo
{
private $value;
function __construct($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}
$values = array(new Foo(1), new Foo(3), new Foo(2));
$sorted = Arrays::sort($values, Comparator::compareBy('getValue()'));
Result:
Array
(
[0] => class Foo (1) {
private $value => int(1)
}
[1] => class Foo (1) {
private $value => int(2)
}
[2] => class Foo (1) {
private $value => int(3)
}
)