sort() 공개 정적인 메소드

The comparator function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. To obtain comparator one may use Comparator class (for instance Comparator::natural() which yields ordering using comparison operators). Example: class Foo { private $value; function __construct($value) { $this->value = $value; } public function getValue() { return $this->value; } } $values = array(new Foo(1), new Foo(3), new Foo(2)); $sorted = Arrays::sort($values, Comparator::compareBy('getValue()')); Result: Array ( [0] => class Foo (1) { private $value => int(1) } [1] => class Foo (1) { private $value => int(2) } [2] => class Foo (1) { private $value => int(3) } )
public static sort ( array $array, $comparator ) : array
$array array
$comparator
리턴 array sorted according to the comparator
예제 #1
0
 public function sort($comparator)
 {
     $this->_array = Arrays::sort($this->_array, $comparator);
     return $this;
 }
예제 #2
0
 /**
  * @test
  */
 public function shouldSortArrayByDefaultExtractor()
 {
     //given
     $array = array(1, 3, 2);
     //when
     $sorted = Arrays::sort($array, Comparator::natural());
     //then
     $this->assertEquals(array(1, 2, 3), $sorted);
 }