Inital commit after splitting from project

This commit is contained in:
2026-07-21 22:00:16 +02:00
commit f299f2c60e
49 changed files with 3102 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Tez\Utils\Arr;
final class SortBy
{
/**
* Sorts a copy of the array by the value extracted via callback.
* Does not mutate the original array. Result is always re-indexed.
*
* @template TValue
* @param array<TValue> $items
* @param callable(TValue): (int|float|string) $keyFn
* @return list<TValue>
*/
public static function sort(array $items, callable $keyFn, bool $descending = false): array
{
$copy = array_values($items);
usort($copy, static function ($a, $b) use ($keyFn, $descending): int {
$ka = $keyFn($a);
$kb = $keyFn($b);
$cmp = is_string($ka) ? strcmp((string) $ka, (string) $kb) : ($ka <=> $kb);
return $descending ? -$cmp : $cmp;
});
return $copy;
}
}