# Dynamische Sortierung zur Laufzeit aus Developer Guide funktioniert nicht

**URL:** <https://forum.shopware.com/t/dynamische-sortierung-zur-laufzeit-aus-developer-guide-funktioniert-nicht/106353>\
**Category:** Shopware 6 (German)\
**Created:** [27. Januar 2025 um 09:13 UTC](https://forum.shopware.com/t/dynamische-sortierung-zur-laufzeit-aus-developer-guide-funktioniert-nicht/106353 "2025-01-27T09:13:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomole](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@tomole](https://forum.shopware.com/u/tomole)\
**Post date:** [27. Januar 2025 um 09:13 UTC](https://forum.shopware.com/t/dynamische-sortierung-zur-laufzeit-aus-developer-guide-funktioniert-nicht/106353/1 "2025-01-27T09:13:30Z")

</div>

Hallo,  
ich möchte eine dynamische Sortierung zur Laufzeit [nach dem Developer Guide](https://developer.shopware.com/docs/guides/plugins/plugins/storefront/add-custom-sorting-product-listing.html#create-individual-sorting-at-runtime) verwenden. Dazu habe ich ein neues Plugin generieren lassen und den folgenden Subscriber hinzugefügt. Die beiden neuen Sortieroptionen `My Custom Sorting ASC` und `My Custom Sorting DESC` tauchen auch im Dropdown auf. Ich würde beim Auswählen der Optionen eine Sortierung nach Produktnamen aufsteigend/absteigend erwarten. Die Ergebnisliste ist jedoch jeweils die gleiche. Hat jemand einen Hinweis? Vielen Dank!

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/3X/3/1/31fbad82bd95456ad8e3978b2c50ddbd3db6d0a7.png)

```auto
<?php declare(strict_types=1);

namespace ExampleSort\Subscriber;

use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingCollection;
use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingEntity;
use Shopware\Core\Framework\Uuid\Uuid;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class CustomFilterSubscriber implements EventSubscriberInterface {

    public static function getSubscribedEvents(): array
    {
        return [
            ProductListingCriteriaEvent::class => ['addMyCustomSortingToStorefront', 500],
        ];
    }

    public function addMyCustomSortingToStorefront(ProductListingCriteriaEvent $event): void 
    {
        /** @var ProductSortingCollection $availableSortings */
        $availableSortings = $event->getCriteria()->getExtension('sortings') ?? new ProductSortingCollection();
        
        $myCustomSortingDesc = new ProductSortingEntity();
        $myCustomSortingDesc->setId(Uuid::randomHex());
        $myCustomSortingDesc->setActive(true);
        $myCustomSortingDesc->setTranslated(['label' => 'My Custom Sorting DESC']);
        $myCustomSortingDesc->setKey('my-custom-sort-desc');
        $myCustomSortingDesc->setPriority(1);
        $myCustomSortingDesc->setFields([
            [
                'field' => 'product.name',
                'order' => 'desc', 
                'priority' => 1,
                'naturalSorting' => 0,
            ],
        ]);

        $myCustomSortingAsc = new ProductSortingEntity();
        $myCustomSortingAsc->setId(Uuid::randomHex());
        $myCustomSortingAsc->setActive(true);
        $myCustomSortingAsc->setTranslated(['label' => 'My Custom Sorting ASC']);
        $myCustomSortingAsc->setKey('my-custom-sort-asc');
        $myCustomSortingAsc->setPriority(1);
        $myCustomSortingAsc->setFields([
            [
                'field' => 'product.name',
                'order' => 'asc',
                'priority' => 1,
                'naturalSorting' => 0,
            ],
        ]);
        
        $availableSortings->add($myCustomSortingDesc);
        $availableSortings->add($myCustomSortingAsc);
        
        $event->getCriteria()->addExtension('sortings', $availableSortings);
    }
}

```
