Skip to content

Instantly share code, notes, and snippets.

@tronsha
Last active November 19, 2018 12:24
Show Gist options
  • Save tronsha/8f6b3742cea6fb0fe26e9fe50a301a6c to your computer and use it in GitHub Desktop.
Save tronsha/8f6b3742cea6fb0fe26e9fe50a301a6c to your computer and use it in GitHub Desktop.
<?php
class Sort
{
public const SORT_ASC = 1;
public const SORT_DESC = -1;
public static function sortObjectsByProperty(array $objects, array $properties = ['id'], $direction = self::SORT_ASC): array
{
usort(
$objects,
function ($first, $second) use ($properties, $direction) {
foreach ($properties as $property) {
$property = ucfirst($property);
$first = $first->{'get' . $property}();
$second = $second->{'get' . $property}();
}
return ($first <=> $second) * $direction;
}
);
return $objects;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment