سؤال

This is my collection function:

public function getCollection()
{
    $searchCriteria = $this->searchCriteriaBuilder->addFilter('status', '1')->create();

    $someCollection = $this->someRepositoryInterface->getList($searchCriteria);

    return $someCollection->getItems();

}

So far everything works fine.

The filter works with no issues, all enabled items are displaying.

I need help with sorting. I have a field called "position". I need to display items in ascending way, what I mean is "position 1" should be first displayed item, second should be under first, etc.

As I suppose there should be an easy method for that but can't find it...

Any suggestions will be highly appreciated.

هل كانت مفيدة؟

المحلول

You will need to add sort order to search criteria

Add \Magento\Framework\Api\SortOrderBuilder $sortOrderBuilder as dependency in constructor.

public function getCollection()
    {
        $sortOrder = $this->sortOrderBuilder->setField('position')->setDirection('DESC')->create();

        $searchCriteria = $this->searchCriteriaBuilder->addFilter('status', '1')->setSortOrders([$sortOrder])->create();

        $someCollection = $this->someRepositoryInterface->getList($searchCriteria);

        return $someCollection->getItems();

    }

Let me know in case you face any issue

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top