Skip to content

Magento2.4.2: How to sort product by custom price attribute?

I want to sort product collection by Custom Price attribute.
enter image description here
here is my code

Here is my code, what is the problem here, i dont understand?

<?php
namespace VendorModuleModelVisualMerchandiserSorting;
use MagentoCatalogModelResourceModelProductCollection;
use MagentoVisualMerchandiserModelSortingSortAbstract;
use MagentoVisualMerchandiserModelSortingSortInterface;
class CustomPriceBottom extends SortAbstract implements SortInterface
{
    /**
     * @param Collection $collection
     * @return Collection
     */
    public function sort(
        Collection $collection
    ): Collection {
        $collection->addAttributeToSelect('*')
            ->setOrder('custom_price', 'DESC');
        
//        $collection->getSelect()
//            ->distinct('entity_id')
//            ->reset(Zend_Db_Select::ORDER)
//            ->order('custom_price ' . CollectionAlias::SORT_ORDER_ASC);
        return $collection;
    }
    /**
     * @return string
     */
    public function getLabel(): string
    {
        return __("Custom Price Bottom");
    }
}