public function testComparator() { $cmp = ObjectComparator::init(['score' => 'desc', 'first_name' => _Comparator::alphaNumeric(), 'id']); $data = [GenericObject::init(['score' => 99, 'first_name' => 'asc', 'id' => 5]), GenericObject::init(['score' => 56, 'first_name' => 'dsf', 'id' => 3]), GenericObject::init(['score' => 99, 'first_name' => 'asc', 'id' => 1]), GenericObject::init(['score' => 99, 'first_name' => 'fgh', 'id' => 2]), GenericObject::init(['score' => 99, 'first_name' => 'xxx', 'id' => 4])]; $result = [GenericObject::init(['score' => 99, 'first_name' => 'asc', 'id' => 1]), GenericObject::init(['score' => 99, 'first_name' => 'asc', 'id' => 5]), GenericObject::init(['score' => 99, 'first_name' => 'fgh', 'id' => 2]), GenericObject::init(['score' => 99, 'first_name' => 'xxx', 'id' => 4]), GenericObject::init(['score' => '56', 'first_name' => 'dsf', 'id' => 3])]; $sort = _Array::sort($cmp); $this->assertEquals($result, $sort($data)); }
public function __invoke(...$data) { $sort = _Array::sort(_Comparator::numeric()); $arr = $sort($data[0]); $length = count($arr); if ($length % 2) { return $arr[(int) floor($length / 2)]; } return ($arr[(int) floor($length / 2)] + $arr[(int) floor($length / 2) - 1]) / 2; }
public function testStableSort2() { $stableSort = _Array::stableSort(_Comparator::alphaNumeric()->map(function ($elem) { return $elem['b']; }), function ($a, $b) { return $a['a'] - $b['a']; }); $this->assertInstanceOf(ArrayStableSort::class, $stableSort); $data = ['p' => ['a' => 34, 'b' => 'p1'], 's' => ['a' => 32, 'b' => 's1'], 'l' => ['a' => 32, 'b' => 'l1'], 'm' => ['a' => 31, 'b' => 'm1'], 'n' => ['a' => 23, 'b' => 'm1']]; $this->assertEquals(json_encode($stableSort($data)), json_encode(['l' => ['a' => 32, 'b' => 'l1'], 'n' => ['a' => 23, 'b' => 'm1'], 'm' => ['a' => 31, 'b' => 'm1'], 'p' => ['a' => 34, 'b' => 'p1'], 's' => ['a' => 32, 'b' => 's1']])); }
public function testBooleanInv() { $cmpNode = _Comparator::boolean()->invert(); $data = [true, false, true, false, true, false, true]; $node = _Array::values()->sort($cmpNode); $this->assertEquals([true, true, true, true, false, false, false], $node($data)); $this->assertInstanceOf(AbstractComparator::class, $cmpNode); }
//int(10) $explode = _String::explode('.'); var_dump($explode('a.b')); $chain = $explode->implode('-'); var_dump($chain('a.b')); $map = $explode->map($fromHex)->implode('.'); var_dump($map('a.b')); $sort = _Array::sort(function ($a, $b) { return $a - $b; }); var_dump($sort([2, 5, 3, 4, 1])); //array(5) { // [0]=> // int(1) // [1]=> // int(2) // [2]=> // int(3) // [3]=> // int(4) // [4]=> // int(5) //} $string = 'a,3,e,22,a2,3e0,cf'; $explodeHexString = _Array::explode(',')->map(_String::trim()->then($fromHex))->sort(_Comparator::numeric()); print_r($explodeHexString($string)); $string = 'a,3,e,22,a2,3e0,cf'; $explodeHexString = _Array::explode(',')->sort(_Comparator::boolean()->map(_String::trim()->then($fromHex)->then(function ($e) { return $e % 5; })), _Comparator::numeric()->map(_String::trim()->then($fromHex))); print_r($explodeHexString($string));