I am doing the following to sort the product collection based on an attribute whose values are actually of type "String" but can only hold 0 or 1. I also mentioned default value as 0.

$collection = $this->getCollection()
    ->addAttributeToSort('attr_name' *1, 'DESC');

In the above code, since the attribute type is "string", I added *1 to convert the string type to Integer as mentioned in this post.

What should I do to sort the product collection based on a attribute which is of type string but has only values 0 and 1?

PS: The collection is being sorted with other attribute in __construct function already and then made to setCollection($collection).

1 Answer 1


Actually the fact that it's a string as value in the database shouldn't matter. MySQL will ignore the type.

So you can just add the attr_name and the direction of the sorting.

$this->getCollection()->addAttributeToSort('attr_name', 'DESC');

When using flat product data tables please make sure the attribute is added to the table. This can be done on the attribute edit page by turning Used in Product Listing on.

To debug it might help to output or log the query and optionally execute it in, for example, PHPMyAdmin to see what happens and tweak it quickly to get the desired results

  • I did the same.. the attribute values for each collection are coming like 0, 1, 0, 0, 0, 0... when I used DESC instead of 1, 0, 0, 0, 0,....
    – Mr_Green
    Commented Jul 9, 2014 at 11:41
  • Thanks but it is not flat product data tables..
    – Mr_Green
    Commented Jul 9, 2014 at 11:44
  • updated my answer :) Commented Jul 9, 2014 at 11:47
  • I did the Mage::log as you explained. I got a big sql query. Nothing is happening based on my attribute in that sql query.
    – Mr_Green
    Commented Jul 10, 2014 at 5:45
  • Sorry, there is also my attribute name in that sql query but I can't see collection in frontend. The count is also visible as 1 instead of 21.
    – Mr_Green
    Commented Jul 10, 2014 at 5:55

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.