<?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
('number', 0));
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+ICcwMy8xMS8xOTg3JyksCiAgICBhcnJheSgnY2MnLCAnbmFtZScgPT4gJ0hlbGVuJywgJ251bWJlcicgPT4gNDQsICdiaXJ0aGRheScgPT4gJzI0LzA2LzE5NjcnKSwKKTsKCnVzb3J0KCRkYXRhLCBtYWtlX2NvbXBhcmVyKCdudW1iZXInLCAwKSk7CnByaW50X3IoJGRhdGEpOwoKCmZ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Sk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICAkbGhzID0gJGZpcnN0WyRjb2x1bW5dOwogICAgICAgICAgICAgICAgJHJocyA9ICRzZWNvbmRbJGNvbHVtbl07CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIC8vIERvIHRoZSBhY3R1YWwgY29tcGFyaXNvbjsgZG8gbm90IHJldHVybiBpZiBlcXVhbAogICAgICAgICAgICBpZiAoJGxocyA8ICRyaHMpIHsKICAgICAgICAgICAgICAgIHJldHVybiAtMSAqICRzb3J0T3JkZXI7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZiAoJGxocyA+ICRyaHMpIHsKICAgICAgICAgICAgICAgIHJldHVybiAxICogJHNvcnRPcmRlcjsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgcmV0dXJuIDA7IC8vIHRpZWJyZWFrZXJzIGV4aGF1c3RlZCwgc28gJGZpcnN0ID09ICRzZWNvbmQKICAgIH07Cn0=