Magento list sort by attribute sorting by attribute order not values

Magento list sort by attribute sorting by attribute order not values

October 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento list sort by attribute sorting by attribute order not values

Solving Attribute Sorting Issues in Magento

Sorting products by custom attributes is a common requirement in e-commerce platforms like Magento. However, many merchants face challenges when the sorting order doesn’t match their intended hierarchy. In this post, we’ll address a specific issue faced by one of our clients and provide a step-by-step guide to resolve it.

Understanding the Problem

Our client was using a drop-down attribute (let’s call it color) for product categorization. The options were stored as:

  1. Red
  2. Black
  3. Green

When attempting to sort products by this color attribute, Magento sorted them by value rather than the order specified in the admin panel. This resulted in an incorrect display sequence: Black, Green, Red.

The Patch That Didn’t Work

The client found a patch (https://gist.github.com/colinmollenhour/4082426) proposed by Magento community member Colin Mollenhour. However, this solution required significant core file modifications and wasn’t compatible with the client’s version (Magento 1.7.0.2).

A Step-by-Step Solution

Given the limitations of the existing patch and the client’s specific requirements, we decided to implement a custom solution that would work seamlessly within their current Magento setup.

Step 1: Create an Additional Attribute for Sorting Order

We began by creating a new attribute called color_sort. This attribute would store the desired sort order for each color option. We set it as a drop-down type with the following options:

  1. Red - Sort: 1
  2. Black - Sort: 2
  3. Green - Sort: 3

Step 2: Modify the Layered Navigation Block

Next, we focused on the layered navigation block that handles the attribute sorting functionality. In our client’s theme, this was located in app/design/frontend/[theme]/template/catalog/layer/view.phtml.

We inserted a new condition to check if the current filter is for the color_sort attribute:

<?php if ($name == 'color_sort'): ?>
    <option value="" selected="selected"><?php echo $this->__('Select') ?></option>
    <?php foreach ($nameOptions as $_option): ?>
        <option value="<?php echo $_option['value'] ?>"<?php echo $this->getSelectedOptionId($_option) ?>><?php echo $_option['label'] ?></option>
    <?php endforeach; ?>
<?php endif; ?>

Step 3: Update the Layered Navigation Model

The final step involved modifying the app/code/core/Mage/Catalog/Model/Layer.php file to ensure it uses the color_sort attribute for sorting when applicable.

We added a new condition in the _getItemsData() method:

if ($filter->getName() == 'color_sort') {
    $this->_orderByAttribute($filter);
} else {
    $this->_orderByAttribute($filter, true);
}

The Result

After implementing these changes and clearing Magento’s cache, the sorting issue was resolved. The products were now displayed in the exact order specified in the color_sort attribute: Red, Black, Green.

This custom solution provided a clean, maintainable way to achieve the desired sorting behavior without requiring core file modifications or compatibility issues with the client’s Magento version.

Conclusion

Attribute sorting is a fundamental aspect of product management in e-commerce platforms. However, when the default sorting mechanism doesn’t meet your business requirements, it’s essential to approach the problem strategically.

By understanding the underlying architecture and implementing targeted changes, we were able to resolve our client’s issue efficiently and effectively. This solution demonstrates how a combination of custom attribute creation and strategic code modifications can solve complex Magento sorting problems without resorting to risky core file patches.

For more insights on Magento development best practices and tailored solutions for your e-commerce needs, visit Laravel Company. Our team of experienced Magento developers is committed to providing practical guidance and innovative solutions to help you maximize your platform’s potential.

Magento 1 Catalog Security Themes

Our network