/** * Sort a array by value, by a closure or by a property. * * - If the sorter is null, the array is sorted naturally. * - Associative (string) keys will be maintained, but numeric keys will be re-indexed. * * @param null $sorter * @param string|int $direction * @param int $strategy * * @return Arrayy (Immutable) */ public function sorter($sorter = null, $direction = SORT_ASC, $strategy = SORT_REGULAR) { $array = (array) $this->array; $direction = $this->getDirection($direction); // Transform all values into their results. if ($sorter) { $arrayy = new self($array); $that = $this; $results = $arrayy->each(function ($value) use($sorter, $that) { return is_callable($sorter) ? $sorter($value) : $that->get($sorter, null, $value); }); $results = $results->getArray(); } else { $results = $array; } // Sort by the results and replace by original values array_multisort($results, $direction, $strategy, $array); return static::create($array); }