I will add my own version, but most likely I will consider your suggestions. Despite what I have tested, it seems to be faster, and it eats less memory.
In order to avoid unnecessary work, you can cache the compared values, as shown in the example
function pc_array_sort($array, $map_func, $sort_func = '') { $mapped = array_map($map_func, $array); // cache $map_func() values if ('' == $sort_func) { asort($mapped); // функция asort() быстрее функции usort() } else { uasort($mapped, $sort_func); // необходимо сохранить ключи } while (list($key) = each($mapped)) { $sorted[] = $array[$key]; // используем отсортированные ключи } return $sorted; }
To avoid unnecessary work, the pc_array_sort () function uses a temporary $ mapped array to cache the returned values. It then sorts the $ mapped array using either the default sorting order or a user-defined sorting procedure. It is important that it uses a sort that preserves key / value relationships. By default, it uses the asort () function, because it is faster than the uasort () function. (The slowness of the uasort () function is certainly a significant argument in favor of the pc_array_sort () function.) Finally, it creates a sorted array of $ sort ed, while the sorted keys in the $ mapped array act as indices of the values of the original array. For small arrays or short sorting functions, the usort () function is faster, but as soon as the number of comparisons grows, the pc_array_sort () function overtakes the usort () function.
Well, or compare in the first example as advised as follows
$date1 = DateTime::createFromFormat('!Y/m/d', '2012/10/17'); $date2 = DateTime::createFromFormat('!Y/m/d', '2012/10/17'); var_dump($date1 == $date2); //will be true var_dump($date1 > $date2); //will be false var_dump($date1 < $date2); //will be false