Skip to content

Instantly share code, notes, and snippets.

@sneznaovca
Created March 13, 2016 20:48
Show Gist options
  • Save sneznaovca/3ce8d6fbd74bcbd28018 to your computer and use it in GitHub Desktop.
Save sneznaovca/3ce8d6fbd74bcbd28018 to your computer and use it in GitHub Desktop.
sort.php
/**
* @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