public function sortBy($criteria)
    {
        $criteria = $this->factoryCallableCriteria($criteria);
        $sorted   = $this->create();
        $groups   = $this->groupBy($criteria);

        $criterias = $this->map(function ($element, $key) use ($criteria) {
            return $criteria($element, $key);
        })->toArray();

        sort($criterias);
        $criterias = array_unique($criterias);

        foreach ($criterias as $key) {
            foreach ($groups[$key] as $element) {
                $sorted[] = $element;
            }
        }

        return $sorted;
    }
public function sortBy($criteria)
    {
        $criteria = $this->factoryCallableCriteria($criteria);
        $sorted   = $this->create();
        $groups   = $this->groupBy($criteria);

        $criterias = $this->map(function ($element, $key) use ($criteria) {
            return $criteria($element, $key);
        })->toArray();

        sort($criterias);
        $criterias = array_unique($criterias);

        foreach ($criterias as $key) {
            foreach ($groups[$key] as $element) {
                $sorted[] = $element;
            }
        }

        return $sorted;
    }
public function sortBy ( $ criteria ) { $ criteria = $ this -> factoryCallableCriteria ( $ criteria ) ; $ sorted = $ this -> create ( ) ; $ groups = $ this -> groupBy ( $ criteria ) ; $ criterias = $ this -> map ( function ( $ element , $ key ) use ( $ criteria ) { return $ criteria ( $ element , $ key ) ; } ) -> toArray ( ) ; sort ( $ criterias ) ; $ criterias = array_unique ( $ criterias ) ; foreach ( $ criterias as $ key ) { foreach ( $ groups [ $ key ] as $ element ) { $ sorted [ ] = $ element ; } } return $ sorted ; }
Sort elements by the given criteria

@param mixed $criteria it can be either a callable function or a string,
representing a key of an element

@return Hash
Sort elements by the given criteria