EntityRepositoryHelperTrait.php
public function sortCollection(iterable $collection, $indexBy = null): iterable
    {
        $this->checkRepository();

        $finalCollection = [];
        $currentObject   = current($collection);
        $accessor        = class_exists('Symfony\Component\PropertyAccess\PropertyAccess') ? PropertyAccess::createPropertyAccessor() : null;

        if ('_primary' === $indexBy || true === $indexBy) {
            $indexBy = $this->getClassMetadata()->getSingleIdentifierFieldName();
        }

        if (is_object($currentObject) && property_exists($currentObject, $indexBy) && method_exists($currentObject, 'get'.ucfirst($indexBy))) {

            // Sorts a list of objects only if the property and its getter exist
            foreach ($collection as $entity) {
                $id = $accessor ? $accessor->getValue($entity, $indexBy) : $entity->{'get'.ucFirst($indexBy)};
                $finalCollection[$id] = $entity;
            }
            return $finalCollection;
        }

        if (is_array($currentObject) && array_key_exists($indexBy, $currentObject)) {
            // Sorts a list of arrays only if the key exists
            foreach ($collection as $array) {
                $finalCollection[$array[$indexBy]] = $array;
            }
            return $finalCollection;
        }

        if ($collection) {
            throw new \InvalidArgumentException('The collection to sort by index seems to be invalid.');
        }

        return $collection;
    }
public function sortCollection(iterable $collection, $indexBy = null): iterable
    {
        $this->checkRepository();

        $finalCollection = [];
        $currentObject   = current($collection);
        $accessor        = class_exists('Symfony\Component\PropertyAccess\PropertyAccess') ? PropertyAccess::createPropertyAccessor() : null;

        if ('_primary' === $indexBy || true === $indexBy) {
            $indexBy = $this->getClassMetadata()->getSingleIdentifierFieldName();
        }

        if (is_object($currentObject) && property_exists($currentObject, $indexBy) && method_exists($currentObject, 'get'.ucfirst($indexBy))) {

            // Sorts a list of objects only if the property and its getter exist
            foreach ($collection as $entity) {
                $id = $accessor ? $accessor->getValue($entity, $indexBy) : $entity->{'get'.ucFirst($indexBy)};
                $finalCollection[$id] = $entity;
            }
            return $finalCollection;
        }

        if (is_array($currentObject) && array_key_exists($indexBy, $currentObject)) {
            // Sorts a list of arrays only if the key exists
            foreach ($collection as $array) {
                $finalCollection[$array[$indexBy]] = $array;
            }
            return $finalCollection;
        }

        if ($collection) {
            throw new \InvalidArgumentException('The collection to sort by index seems to be invalid.');
        }

        return $collection;
    }
public function sortCollection ( iterable $ collection , $ indexBy = null ) : iterable { $ this -> checkRepository ( ) ; $ finalCollection = [ ] ; $ currentObject = current ( $ collection ) ; $ accessor = class_exists ( 'Symfony\Component\PropertyAccess\PropertyAccess' ) ? PropertyAccess :: createPropertyAccessor ( ) : null ; if ( '_primary' === $ indexBy || true === $ indexBy ) { $ indexBy = $ this -> getClassMetadata ( ) -> getSingleIdentifierFieldName ( ) ; } if ( is_object ( $ currentObject ) && property_exists ( $ currentObject , $ indexBy ) && method_exists ( $ currentObject , 'get' . ucfirst ( $ indexBy ) ) ) { // Sorts a list of objects only if the property and its getter exist foreach ( $ collection as $ entity ) { $ id = $ accessor ? $ accessor -> getValue ( $ entity , $ indexBy ) : $ entity -> { 'get' . ucFirst ( $ indexBy ) } ; $ finalCollection [ $ id ] = $ entity ; } return $ finalCollection ; } if ( is_array ( $ currentObject ) && array_key_exists ( $ indexBy , $ currentObject ) ) { // Sorts a list of arrays only if the key exists foreach ( $ collection as $ array ) { $ finalCollection [ $ array [ $ indexBy ] ] = $ array ; } return $ finalCollection ; } if ( $ collection ) { throw new \ InvalidArgumentException ( 'The collection to sort by index seems to be invalid.' ) ; } return $ collection ; }
Sorts a collection by a specific key, usually the primary key one,
but you can specify any key.
For "cleanest" uses, you'd better use a primary or unique key.
Sorts a collection by a specific key usually the primary key one but you can specify any key . For cleanest uses you d better use a primary or unique key .