I like it. My first thought was to declare all of the closures (allowing you to use the 'newtoold' closure inside your 'mosthelpful'/'leasthelpful' closures), and do away with the switch statement:
function applySort_revised( array $data,$orderBy='newtoold' ){
$oldtonew = function($a, $b) {
return strtotime($a['review_timestamp']) - strtotime($b['review_timestamp']);
};
$newtoold = function($a, $b) {
return strtotime($b['review_timestamp']) - strtotime($a['review_timestamp']);
};
$mosthelpful = function( $a,$b )use( $newtoold ){
$diff = strtotime($b['likes']) - strtotime($a['likes']);
return ($diff != 0) ?
$diff :
$newtoold( $a,$b );
};
$leasthelpful = function($a, $b)use( $newtoold ){
$diff = strtotime($b['dislikes']) - strtotime($a['dislikes']);
return ($diff != 0) ?
$diff :
$newtoold( $a,$b );
};
if( empty( $$orderBy ) ){ $orderBy = 'newtoold'; }
usort($data, $$orderBy);
return $data;
}
This offers similar performance, so there's no clear advantage.
However, neither method seems to scale well (testing 1,000 iterations each, I hit max execution time with only 50 elements* in the $data array).
*which I assumed looked something like this (using arbitrary values):
[
'likes' => '5/10/12'
,'dislikes' => '1/3/12'
,'review_timestamp' => '10/1/12'
]
BTW, why are 'likes' and 'dislikes' time values?[/i]