<?php
array('zz', 'name' => 'Jack', 'number' => 22, 'birthday' => '12/03/1980'), array('xx', 'name' => 'Adam', 'number' => 16, 'birthday' => '01/12/1979'), array('aa', 'name' => 'Paul', 'number' => 16, 'birthday' => '03/11/1987'), array('cc', 'name' => 'Helen', 'number' => 44, 'birthday' => '24/06/1967'), );
usort($data, make_comparer
(['birthday', SORT_ASC
, 'date_create']));
function make_comparer() {
// Normalize criteria up front so that the comparer finds everything tidy
foreach ($criteria as $index => $criterion) {
$criteria[$index] = is_array($criterion) : array($criterion, SORT_ASC
, null); }
return function($first, $second) use ($criteria) {
foreach ($criteria as $criterion) {
// How will we compare this round?
list($column, $sortOrder, $projection) = $criterion; $sortOrder = $sortOrder === SORT_DESC ? -1 : 1;
// If a projection was defined project the values now
if ($projection) {
}
else {
$lhs = $first[$column];
$rhs = $second[$column];
}
// Do the actual comparison; do not return if equal
if ($lhs < $rhs) {
return -1 * $sortOrder;
}
else if ($lhs > $rhs) {
return 1 * $sortOrder;
}
}
return 0; // tiebreakers exhausted, so $first == $second
};
}
PD9waHAKCiRkYXRhID0gYXJyYXkoCiAgICBhcnJheSgnenonLCAnbmFtZScgPT4gJ0phY2snLCAnbnVtYmVyJyA9PiAyMiwgJ2JpcnRoZGF5JyA9PiAnMTIvMDMvMTk4MCcpLAogICAgYXJyYXkoJ3h4JywgJ25hbWUnID0+ICdBZGFtJywgJ251bWJlcicgPT4gMTYsICdiaXJ0aGRheScgPT4gJzAxLzEyLzE5NzknKSwKICAgIGFycmF5KCdhYScsICduYW1lJyA9PiAnUGF1bCcsICdudW1iZXInID0+IDE2LCAnYmlydGhkYXknID0+ICcwMy8xMS8xOTg3JyksCiAgICBhcnJheSgnY2MnLCAnbmFtZScgPT4gJ0hlbGVuJywgJ251bWJlcicgPT4gNDQsICdiaXJ0aGRheScgPT4gJzI0LzA2LzE5NjcnKSwKKTsKCnVzb3J0KCRkYXRhLCBtYWtlX2NvbXBhcmVyKFsnYmlydGhkYXknLCBTT1JUX0FTQ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HJocyA9IGNhbGxfdXNlcl9mdW5jKCRwcm9qZWN0aW9uLCAkc2Vjb25kWyRjb2x1bW5dKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIHsKICAgICAgICAgICAgICAgICRsaHMgPSAkZmlyc3RbJGNvbHVtbl07CiAgICAgICAgICAgICAgICAkcmhzID0gJHNlY29uZFskY29sdW1uXTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgLy8gRG8gdGhlIGFjdHVhbCBjb21wYXJpc29uOyBkbyBub3QgcmV0dXJuIGlmIGVxdWFsCiAgICAgICAgICAgIGlmICgkbGhzIDwgJHJocykgewogICAgICAgICAgICAgICAgcmV0dXJuIC0xICogJHNvcnRPcmRlcjsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmICgkbGhzID4gJHJocykgewogICAgICAgICAgICAgICAgcmV0dXJuIDEgKiAkc29ydE9yZGVyOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICByZXR1cm4gMDsgLy8gdGllYnJlYWtlcnMgZXhoYXVzdGVkLCBzbyAkZmlyc3QgPT0gJHNlY29uZAogICAgfTsKfQ==