lithium\data\Collection::sort()

public method

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.

Parameters

  • string|callable $field

    The field to sort the data on, can also be a callback to a custom sort function.

  • array $options

    Reserved for future use.

Returns

lithium\data\Collection

Returns itself.

Source

	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);
	}