<?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
('name'));
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+ICcwMy8xMS8xOTg3JyksCiAgICBhcnJheSgnY2MnLCAnbmFtZScgPT4gJ0hlbGVuJywgJ251bWJlcicgPT4gNDQsICdiaXJ0aGRheScgPT4gJzI0LzA2LzE5NjcnKSwKKTsKCnVzb3J0KCRkYXRhLCBtYWtlX2NvbXBhcmVyKCduYW1lJykpOwpwcmludF9yKCRkYXRhKTsKCgpmdW5jdGlvb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ICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgJGxocyA9ICRmaXJzdFskY29sdW1uXTsKICAgICAgICAgICAgICAgICRyaHMgPSAkc2Vjb25kWyRjb2x1bW5dOwogICAgICAgICAgICB9CgogICAgICAgICAgICAvLyBEbyB0aGUgYWN0dWFsIGNvbXBhcmlzb247IGRvIG5vdCByZXR1cm4gaWYgZXF1YWwKICAgICAgICAgICAgaWYgKCRsaHMgPCAkcmhzKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gLTEgKiAkc29ydE9yZGVyOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYgKCRsaHMgPiAkcmhzKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gMSAqICRzb3J0T3JkZXI7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIHJldHVybiAwOyAvLyB0aWVicmVha2VycyBleGhhdXN0ZWQsIHNvICRmaXJzdCA9PSAkc2Vjb25kCiAgICB9Owp9Cg==