Collections\Collection::sort PHP Method

sort() public method

public sort ( callable $callback )
$callback callable
    public function sort(callable $callback)
    {
        $items = $this->items;
        usort($items, $callback);
        $col = new static($this->type);
        $col->setItemsFromTrustedSource($items);
        return $col;
    }

Usage Example

Example #1
0
 public function test_sorts_with_callback()
 {
     $col = new Collection('int', [3, 1, 4, 2]);
     $comparator = function ($a, $b) {
         if ($a == $b) {
             return 0;
         }
         return $a < $b ? -1 : 1;
     };
     $sorted = $col->sort($comparator);
     $this->assertEquals(1, $sorted->at(0));
     $this->assertEquals(2, $sorted->at(1));
     $this->assertEquals(3, $sorted->at(2));
     $this->assertEquals(4, $sorted->at(3));
     //collection is unchanged
     $this->assertEquals([3, 1, 4, 2], $col->toArray());
 }