title: Magento - Changing attribute sorting
body: <p>I'm trying to change the sorting of my products. </p>
  
  <p>I want to use the attribute sort by options not to sort by name but rather sort by position given in the "manage attributes". But only when there's a position (when all is 0 it should sort by name).</p>
  
  <p>For this I need to know in which file the sorting is applied to the product collection. I searched inside app\code\core\Mage\Catalog\Model\Config.php and app\code\core\Mage\Catalog\Block\Product\List.php but neither found anything about the sorting. And maybe some code suggestions, too.</p>
  
  <p>Google isn't helping me with this issue, too so I try it here. Maybe somone can help me with this.</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>As example we take the "Color" attribute, it's a drop-down attribute.</p>
  
  <p>The table inside "manage attributes" looks like this:</p>
  
  <pre><code>Valuename   |   Position
  ========================
  light blue  |       1
  blue        |       2
  dark blue   |       3
  light red   |       4
  red         |       5
  dark red    |       6
  </code></pre>
  
  <p>In the frontend I sort by the attribute "Color" and my products will be listed this way:</p>
  
  <pre><code>blue
  dark blue
  dark red
  light blue
  light red
  red
  </code></pre>
  
  <p>But I want it to be listed like this:</p>
  
  <pre><code>light blue
  blue
  dark blue
  light red
  red
  dark red
  </code></pre>
  
  <p><strong>EDIT2:</strong></p>
  
  <pre><code>    public function getAllOptions($withEmpty = true, $defaultValues = false)
      {
          $storeId = $this-&gt;getAttribute()-&gt;getStoreId();
          if (!is_array($this-&gt;_options)) {
              $this-&gt;_options = array();
          }
          if (!is_array($this-&gt;_optionsDefault)) {
              $this-&gt;_optionsDefault = array();
          }
          if (!isset($this-&gt;_options[$storeId])) {
              $collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
                  -&gt;setPositionOrder('asc')
                  -&gt;setAttributeFilter($this-&gt;getAttribute()-&gt;getId())
                  -&gt;setStoreFilter($this-&gt;getAttribute()-&gt;getStoreId())
                  -&gt;load();
              $this-&gt;_options[$storeId]        = $collection-&gt;toOptionArray();
              $this-&gt;_optionsDefault[$storeId] = $collection-&gt;toOptionArray('default_value');
          }
          $options = ($defaultValues ? $this-&gt;_optionsDefault[$storeId] : $this-&gt;_options[$storeId]);
          if ($withEmpty) {
              array_unshift($options, array('label' =&gt; '', 'value' =&gt; ''));
          }
          return $options;
      }
  </code></pre>