Last active
November 19, 2018 12:24
-
-
Save tronsha/8f6b3742cea6fb0fe26e9fe50a301a6c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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