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 testSortAlphaNumericInvert() { $data = ["sgdgd", "adsfd", "fdsgdf", "rgfv", "sfgfsfd"]; $node = _Array::sort(_Comparator::alphaNumeric()->invert()); $this->assertEquals(["sgdgd", "sfgfsfd", "rgfv", "fdsgdf", "adsfd"], $node($data)); }
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']])); }