Exemplo n.º 1
0
 public function testCompareMulti()
 {
     $values = TestData::$fruit;
     usort($values, fn\fnCompareMulti(array(fn\fnCompareField('name'))));
     $this->assertNotEquals(TestData::$fruit, $values);
     $this->assertEquals(Sequence::make(TestData::$fruit)->pluck('name')->sort()->toValues(), array_map(fn\fnPluck('name'), $values));
     $values = array(array('name' => 'Terry', 'age' => 22), array('name' => 'Bob', 'age' => 30), array('name' => 'Ann', 'age' => 30), array('name' => 'Sam', 'age' => 19), array('name' => 'Rob', 'age' => 30), array('name' => 'Robert', 'age' => 55));
     $expected = array(array('name' => 'Robert', 'age' => 55), array('name' => 'Ann', 'age' => 30), array('name' => 'Bob', 'age' => 30), array('name' => 'Rob', 'age' => 30), array('name' => 'Terry', 'age' => 22), array('name' => 'Sam', 'age' => 19));
     usort($values, fn\fnCompareMulti(array(fn\fnCompareFieldRev('age'), fn\fnCompareField('name'))));
     $this->assertEquals($expected, $values);
 }
Exemplo n.º 2
0
/**
 * @param array[] $employees
 * @return array
 */
function exampleKeyByIdSortByEmployeeLastName2($employees)
{
    $employeesSortedById = Sequence::make($employees)->keyBy(fn\fnPluck('employeeId'))->asort(fn\fnCompareField('lastName'))->to_a();
    return $employeesSortedById;
}
Exemplo n.º 3
0
 /**
  * Generates a comparison function that can be used to sort an array by a given field.
  *
  * @param string $fieldName
  * @return Closure
  */
 public static function fnByField($fieldName)
 {
     return fn\fnCompareField($fieldName);
 }