This is my callback for my usort()
public function sortProperties($a, $b) {
$sortA = inflector::camelize(str_replace('-', '_', $this->sortBy));
$sortB = inflector::camelize(str_replace('-', '_', $this->sortBy));
$a = Arr::get($a, $sortA);
$b = Arr::get($b, $sortB);
if (is_numeric($a) AND is_numeric($b)) {
return $a < $b;
} else {
return strcasecmp($a, $b);
}
}
Usually, when I see the first 2 lines in any of my code, it screams to me: refactor! I guess it's because they are identical.
I know I could make a function getCamelized()
, but I don't think I'd use it again outside of this.
Is there a way to turn those 4 lines into 2? Could func_get_args()
or array_walk()
help me here?
Also, is there anything wrong about this sorting function?