Custom Sort of Magento Collection?

Custom Sort of Magento Collection?

September 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Custom Sort of Magento Collection?

Sorting Magento Product Collections by Custom Attribute Values

Sorting product collections based on custom attribute values is a common requirement in e-commerce development. However, Magento’s default sorting mechanisms can sometimes lead to unexpected results, especially when dealing with non-numeric or alphabetical values.

Let’s consider the scenario where you want to sort products based on their ‘size’ attribute. The size values are XS, S, M, L, XL. Using the standard addAttributeToSort method:

$collection->addAttributeToSort('size', 'DESC');

The collection will indeed be sorted by the size attribute, but not necessarily in the desired order (XS < S < M < L < XL). This is because Magento sorts strings alphanumerically, treating XS as greater than S.

Solving the Custom Sorting Challenge

There are a few approaches to solve this issue. The most straightforward method involves creating a new temporary attribute during the select process and sorting by that attribute instead. Here’s how you can achieve this:

  1. Create a new select field during your collection initialization using addFieldToFilter. This field will be a new column in your result set.
$collection->getSelect()->columns(
    [
        'sort_order' => "CASE WHEN `size`='XS' THEN 1 WHEN `size`='S' THEN 2 WHEN `size`='M' THEN 3 WHEN `size`='L' THEN 4 WHEN `size`='XL' THEN 5 END"
    ]
);

This code adds a new virtual column named ‘sort_order’. The CASE statement assigns a numeric value to each size based on your desired sort order.

  1. Sort by the new field. After initializing the collection, you can now sort it using the newly created column:
$collection->addAttributeToSort('sort_order', 'ASC');
  1. Remove unnecessary fields after sorting if needed. If you don’t want the extra sort_order column in your final result set, you can remove it with getSelect()->resetColumns().

Best Practices and Performance Considerations

While this approach works, there are a few things to keep in mind:

  • Performance: Sorting large collections using additional select fields can have performance implications. Make sure to optimize your queries by limiting the number of products retrieved if possible.

Learn more about Magento query optimization best practices at Laravel Company’s guide on Magento Query Performance Tips.

  • Maintenance: Ensure your case statements are up-to-date with any changes in size options to avoid unexpected sorting behavior.

Conclusion

Sorting Magento product collections based on custom attribute values requires a bit of creativity and understanding of the underlying SQL queries. By creating temporary fields during selection and sorting by those fields, you can achieve custom sort orders even for non-numeric attribute values.

Remember to optimize your queries, keep your size options up-to-date, and always test your sorting logic thoroughly before deploying it to production environments.

Happy coding! Let us know if you have any other Magento development questions.

Catalog Orders Errors

Our network