note 74816 deleted from function.usort by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 16:26:11 +0000 |
| Subject: | note 74816 deleted from function.usort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176064@lists.php.net to get a copy of this message | ||
Note Submitter: lite
----
<?php
//
class __partial {
var $args;
var $fn;
function __partial($args) {
$this->fn = array_shift ($args);
$this->args = $args;
}
function right() {
$args = func_get_args ();
array_splice ($args, count($args), 0, $this->args);
return call_user_func_array ($this->fn, $args);
}
function left() {
$args = func_get_args ();
array_splice ($args, 0, 0, $this->args);
return call_user_func_array ($this->fn, $args);
}
}
function rcurry () {
$closure = &new __partial (func_get_args ());
return array ($closure, 'right');
}
function lcurry () {
$closure = &new __partial (func_get_args ());
return array ($closure, 'left');
}
function compare($a, $b, $key) {
if ($a[$key] == $b[$key]) {
return 0;
}
return ($a[$key] > $b[$key]) ? +1 : -1;
}
function sortOn ($array, $key) {
usort ($array, rcurry('compare', $key));
return $array;
}
$array = array (array ('key' => 1), array ('key' => 5), array
('key' => 3));
print_r (sortOn ($array, 'key'));
?>