public function testReverse()
 {
     $this->collection->reverse();
     $this->assertEquals($this->_3, $this->collection->get(0));
     $this->assertEquals($this->_2, $this->collection->get(1));
     $this->assertEquals($this->_1, $this->collection->get(2));
     $this->assertEquals($this->_0, $this->collection->get(3));
 }
 public function testReverse()
 {
     $collection = new Collection(['first' => new Bar('a'), 'second' => new Bar('b'), 'third' => new Bar('c'), 'fourth' => new Bar('c')]);
     $this->assertSame(['fourth', 'third', 'second', 'first'], $collection->reverse()->keys());
 }
Example #3
0
 /**
  * Sort a collection by user preference
  * @param  string/callable $sortBy
  * @param  [string] $sorting
  * @return Collection
  */
 public function sort($sortBy = null, $sorting = 'ascending')
 {
     $tmp = $this->data;
     if (!is_callable($sortBy)) {
         $callback = function ($a, $b) use($sortBy) {
             if (is_array($a) && is_array($b)) {
                 $a = ArrayHelper::path($a, $sortBy);
                 $b = ArrayHelper::path($b, $sortBy);
             }
             return Comparator::compare($a, $b);
         };
     } else {
         $callback = $sortBy;
     }
     uasort($tmp, $callback);
     $newCollection = new Collection($tmp);
     if ($sorting == 'descending') {
         return $newCollection->reverse();
     }
     return $newCollection;
 }