0

Is it possible to sort the product collection in two levels, first sort the collection by position and if products have similar positions then sort by name.

|#|Product Name| position|
|2| bde        | 0       |
|1| bcd        | 1       |
|3| cde        | 3       |
|4| abc        | 3       |

The above is how the products appear on sorting by position but i want it to show as below:

|#|Product Name| position|
|2| bde        | 0       |
|1| bcd        | 1       |
|4| abc        | 3       |
|3| cde        | 3       |

1 Answer 1

1

This should do it:

$collection->addOrder('position', \Magento\Framework\Data\Collection::SORT_ORDER_ASC)
           ->addOrder('name', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);
0

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.