Created
March 13, 2016 20:48
-
-
Save sneznaovca/3ce8d6fbd74bcbd28018 to your computer and use it in GitHub Desktop.
sort.php
This file contains 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
/** | |
* @param array $list | |
* @param string $sortAttribute | |
* @return array | |
*/ | |
public static function by(array $list, $sortAttribute) | |
{ | |
usort($list, function($o1,$o2) use($sortAttribute) { | |
if (!property_exists($o1, $sortAttribute) || !property_exists($o2, $sortAttribute)) { | |
throw new InvalidArgumentException(sprintf('At least one object does not contains attribute \'%s\' used for sorting.', $sortAttribute)); | |
} | |
return strcmp($o1->{$sortAttribute}, $o2->{$sortAttribute}); | |
}); | |
return $list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment