I have a plugin that adds some custom product sorting, based on a customer attribute, in a way similar to below.

# Vendor\ModuleName\Plugin\Product\ProductList\Toolbar;

case 'capacity_low_to_high':
    $collection->setOrder('capacity', 'ASC');

case 'name_high_to_low':
    $collection->setOrder('capacity', 'DESC');

However, the sorting is not as expected. When sorting via the custom 'capacity' attribute, the products are being sorted alphanumerically, such as that the result is someting like:

1 11 19 2 3 4 5

Obviously this is not as desired, but I am unsure of the best way to sort the collection based on the numerical value of the attribute. Can anybody point me in the right direction? Ideally, I'm thinking that storing the capacity attribute as an integer would work best, but there doesn't seem to be an obvious way to implement that. Am I missing something? What are my options. Magento 2.3

  • did you tried addAttributeToSort() function of the collection. ? – nishu Oct 10 '19 at 5:18
  • Have you solved the problem ? If yes then please let me know. – skm Apr 13 at 14:37

Your Answer

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

Browse other questions tagged or ask your own question.