sort() public method

Sorts the objects in the collection.
public sort ( string | callable $sorter = 'sort', array $options = [] ) : Collection
$sorter string | callable The sorter for the data. Either a callable to use as the sort function or a string with the name of a well-known sort function like `'natsort'` or a compare function like `'strcmp'`. Defaults to `'sort'`.
$options array Reserved for future use.
return Collection Returns itself.
Example #1
0
 /**
  * Tests that the Collection::sort method works appropriately.
  */
 public function testCollectionSort()
 {
     $collection = new Collection(array('data' => array(5, 3, 4, 1, 2)));
     $collection->sort();
     $expected = array(1, 2, 3, 4, 5);
     $this->assertEqual($expected, $collection->to('array'));
     $collection = new Collection(array('data' => array('alan', 'dave', 'betsy', 'carl')));
     $expected = array('alan', 'betsy', 'carl', 'dave');
     $this->assertEqual($expected, $collection->sort()->to('array'));
     $collection = new Collection(array('data' => array('Alan', 'Dave', 'betsy', 'carl')));
     $expected = array('Alan', 'betsy', 'carl', 'Dave');
     $this->assertEqual($expected, $collection->sort('strcasecmp')->to('array'));
     $collection = new Collection(array('data' => array(5, 3, 4, 1, 2)));
     $collection->sort(function ($a, $b) {
         if ($a === $b) {
             return 0;
         }
         return $b > $a ? 1 : -1;
     });
     $expected = array(5, 4, 3, 2, 1);
     $this->assertEqual($expected, $collection->to('array'));
     $collection = new Collection(array('data' => array(5, 3, 4, 1, 2)));
     $result = $collection->sort('blahgah');
     $this->assertEqual($collection->to('array'), $result->to('array'));
 }
Example #2
0
 /**
  * Tests that the Collection::sort method works appropriately.
  *
  * @return void
  */
 public function testCollectionSort()
 {
     // Typical numeric sort using the default "sort" PHP function
     $collection = new Collection(array('data' => array(5, 3, 4, 1, 2)));
     $collection->sort();
     $expected = array(1, 2, 3, 4, 5);
     $this->assertEqual($expected, $collection->to('array'));
     // String sort using "sort"
     $collection = new Collection(array('data' => array('alan', 'dave', 'betsy', 'carl')));
     $collection->sort();
     $expected = array('alan', 'betsy', 'carl', 'dave');
     $this->assertEqual($expected, $collection->to('array'));
     // String sort using strcasecmp
     $collection = new Collection(array('data' => array('Alan', 'Dave', 'betsy', 'carl')));
     $collection->sort('strcasecmp');
     $expected = array('Alan', 'betsy', 'carl', 'Dave');
     $this->assertEqual($expected, $collection->to('array'));
     // Numeric sort using custom function
     $collection = new Collection(array('data' => array(5, 3, 4, 1, 2)));
     $collection->sort(function ($a, $b) {
         if ($a == $b) {
             return 0;
         }
         return $b > $a ? 1 : -1;
     });
     $expected = array(5, 4, 3, 2, 1);
     $this->assertEqual($expected, $collection->to('array'));
     // Test fail
     $collection = new Collection(array('data' => array(5, 3, 4, 1, 2)));
     $result = $collection->sort('blahgah');
     $this->assertEqual($collection->to('array'), $result->to('array'));
 }
Example #3
0
 /**
  * Sorts the objects in the collection, useful in situations where
  * you are already using the underlying datastore to sort results.
  *
  * Overriden to load any data that has not yet been loaded.
  *
  * @see lithium\util\Collection::sort()
  * @param string|callable $field The field to sort the data on, can also be a callback
  *        to a custom sort function.
  * @param array $options Reserved for future use.
  * @return lithium\data\Collection Returns itself.
  */
 public function sort($field = 'id', array $options = array())
 {
     $this->offsetGet(null);
     if (is_string($field)) {
         $sorter = function ($a, $b) use($field) {
             if (is_array($a)) {
                 $a = (object) $a;
             }
             if (is_array($b)) {
                 $b = (object) $b;
             }
             return strcmp($a->{$field}, $b->{$field});
         };
     } elseif (is_callable($field)) {
         $sorter = $field;
     } else {
         return $this;
     }
     return parent::sort($sorter, $options);
 }