0

I want to sort a repository by more than one attribute. Any ideas of how to achieve this?

What I have so far is this:

public function getLatestPosts()
{
  return $this->postRepository
   ->getList($this->buildSearchCriteriaForLatestPosts())
   ->getItems();
}

private function buildSearchCriteriaForLatestPosts()
{
  $this->prepareSearchCriteriaBuilder();
  $this->searchCriteriaBuilder->setPageSize(1);
  return $this->searchCriteriaBuilder->create();
}

private function prepareSearchCriteriaBuilder()
{

   /** @var \Magento\Framework\Api\SortOrder $publishDateOrder */
   $publishDateOrder = $this->sortOrderBuilder
      ->setField(PostInterface::PUBLISH_DATE)
      ->setDirection(SortOrder::SORT_DESC)
      ->create();

   /** @var \Magento\Framework\Api\SortOrder $isFeaturedOrder */
   $isFeaturedOrder = $this->sortOrderBuilder
      ->setField('is_featured')
      ->setDirection(SortOrder::SORT_DESC)
      ->create();

   $this->searchCriteriaBuilder->setSortOrders([$publishDateOrder, $isFeaturedOrder]);
}

I dont know why it is not working properly; what I want to achieve is to get the list of Posts sorted by is_featured and created_at in DESC. So even if somehow 2 or more posts are featured, it will display the latest one at the top

Any help will be appreciated! Thanks

2 Answers 2

0

Take this example

public function getLoggedInGroups()
    {
        $notLoggedInFilter[] = $this->filterBuilder
            ->setField(GroupInterface::ID)
            ->setConditionType('neq')
            ->setValue(self::NOT_LOGGED_IN_ID)
            ->create();
        $groupAll[] = $this->filterBuilder
            ->setField(GroupInterface::ID)
            ->setConditionType('neq')
            ->setValue(self::CUST_GROUP_ALL)
            ->create();
        $groupNameSortOrder = $this->sortOrderBuilder
            ->setField('customer_group_code')
            ->setAscendingDirection()
            ->create();
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilters($notLoggedInFilter)
            ->addFilters($groupAll)
            ->addSortOrder($groupNameSortOrder)
            ->create();
        return $this->groupRepository->getList($searchCriteria)->getItems();
    }

Have you tried stacking a second addSortOrder?

1
  • Hi Dominic, thanks for your reply. Yes, I have tried stacking a second addSortOrder, but it still doesn't do the work. The thing is that it works if I just have either one sort or another, but not with both Commented Jan 28, 2020 at 20:05
0

I've found another example but that would suggest you can stack

Here you go

    $typeFilter = $this->filterBuilder
                ->setField(TransactionInterface::TXN_TYPE)
                ->setValue($transactionType)
                ->create();
            $idFilter = $this->filterBuilder
                ->setField(TransactionInterface::PAYMENT_ID)
                ->setValue($paymentId)
                ->create();
            $transactionIdSort = $this->sortOrderBuilder
                ->setField('transaction_id')
                ->setDirection(Collection::SORT_ORDER_DESC)
                ->create();
            $createdAtSort = $this->sortOrderBuilder
                ->setField('created_at')
                ->setDirection(Collection::SORT_ORDER_DESC)
                ->create();
            $entity = current(
                $this->getList(
                    $this->searchCriteriaBuilder
                        ->addFilters([$typeFilter])
                        ->addFilters([$idFilter])
                        ->addSortOrder($transactionIdSort)
                        ->addSortOrder($createdAtSort)
                        ->create()
                )->getItems()
            );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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