1

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.

1

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

  • Thanks, it worked fine! – Rufus May 16 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.