private function setSortParams(
        \Magento\Catalog\Model\Layer $layer,
        \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection
    ) {
        $searchQuery = $this->queryFactory->get();

        if (!$searchQuery->getQueryText() && $layer->getCurrentCategory()) {
            $categoryId = $layer->getCurrentCategory()->getId();
            $sortFilter = ['category.category_id' => $categoryId];
            $collection->addSortFilterParameters('position', 'category.position', 'category', $sortFilter);
        } elseif ($searchQuery->getId()) {
            $sortFilter = ['search_query.query_id' => $searchQuery->getId()];
            $collection->addSortFilterParameters('relevance', 'search_query.position', 'search_query', $sortFilter);
        }

        foreach ($this->catalogConfig->getAttributesUsedForSortBy() as $attributeCode => $attribute) {
            if ($attribute->usesSource()) {
                $sortField = $this->mappingHelper->getOptionTextFieldName($attributeCode);
                $collection->addSortFilterParameters($attributeCode, $sortField);
            }
        }

        return $this;
    }
private function setSortParams(
        \Magento\Catalog\Model\Layer $layer,
        \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection
    ) {
        $searchQuery = $this->queryFactory->get();

        if (!$searchQuery->getQueryText() && $layer->getCurrentCategory()) {
            $categoryId = $layer->getCurrentCategory()->getId();
            $sortFilter = ['category.category_id' => $categoryId];
            $collection->addSortFilterParameters('position', 'category.position', 'category', $sortFilter);
        } elseif ($searchQuery->getId()) {
            $sortFilter = ['search_query.query_id' => $searchQuery->getId()];
            $collection->addSortFilterParameters('relevance', 'search_query.position', 'search_query', $sortFilter);
        }

        foreach ($this->catalogConfig->getAttributesUsedForSortBy() as $attributeCode => $attribute) {
            if ($attribute->usesSource()) {
                $sortField = $this->mappingHelper->getOptionTextFieldName($attributeCode);
                $collection->addSortFilterParameters($attributeCode, $sortField);
            }
        }

        return $this;
    }
private function setSortParams ( \ Magento \ Catalog \ Model \ Layer $ layer , \ Magento \ Catalog \ Model \ ResourceModel \ Collection \ AbstractCollection $ collection ) { $ searchQuery = $ this -> queryFactory -> get ( ) ; if ( ! $ searchQuery -> getQueryText ( ) && $ layer -> getCurrentCategory ( ) ) { $ categoryId = $ layer -> getCurrentCategory ( ) -> getId ( ) ; $ sortFilter = [ 'category.category_id' => $ categoryId ] ; $ collection -> addSortFilterParameters ( 'position' , 'category.position' , 'category' , $ sortFilter ) ; } elseif ( $ searchQuery -> getId ( ) ) { $ sortFilter = [ 'search_query.query_id' => $ searchQuery -> getId ( ) ] ; $ collection -> addSortFilterParameters ( 'relevance' , 'search_query.position' , 'search_query' , $ sortFilter ) ; } foreach ( $ this -> catalogConfig -> getAttributesUsedForSortBy ( ) as $ attributeCode => $ attribute ) { if ( $ attribute -> usesSource ( ) ) { $ sortField = $ this -> mappingHelper -> getOptionTextFieldName ( $ attributeCode ) ; $ collection -> addSortFilterParameters ( $ attributeCode , $ sortField ) ; } } return $ this ; }
Apply sort params to the collection.

@param \Magento\Catalog\Model\Layer                                       $layer      Catalog / search layer.
@param \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection Product collection.

@return $this
Apply sort params to the collection .