title: Magento sort mobile navigation categories
body: <p><strong>Situation</strong><br>
  I have a responsive webshop. When the user is on a mobile phone he still can select which category he wants to navigate to. This is done via a expandable menu. Now I'm trying to organize all the categories on alphabetical order. But I have no idea how I can achieve this. All other answers on stack don't fit my problem.</p>
  
  <p><strong>Question</strong><br>
  How can I organize the categories alphabetically in the expandable menu?</p>
  
  <p>mobinav.php</p>
  
  <pre><code>&lt;?php 
  global $var_yttheme;
  if ( $var_yttheme-&gt;isHomepage() === FALSE ) {
  $homecls='class="nav-home"';
  } else {
  $homecls='class="nav-home active"';
  }
  if($var_yttheme-&gt;getParam("responsive_menu")=='2'){ ?&gt;
  &lt;button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"&gt;&lt;i class="icon-reorder"&gt;&lt;/i&gt;
  &lt;/button&gt;
  &lt;div id="yt_resmenu_collapse" style="height: 0;" class="nav-collapse collapse"&gt;
      &lt;ul class="nav-menu clearfix"&gt;
      &lt;li &lt;?php echo $homecls;?&gt;&gt;
          &lt;a &lt;?php echo $homecls;?&gt; href="&lt;?php echo $this-&gt;getUrl('') ?&gt;" title="Home"&gt;&lt;span&gt;Home&lt;/span&gt;&lt;/a&gt;
      &lt;/li&gt;
      &lt;?php foreach ($this-&gt;getStoreCategories() as $_category): ?&gt;
          &lt;?php echo $this-&gt;drawItem($_category) ?&gt;
      &lt;?php endforeach ?&gt;
      &lt;/ul&gt;
      &lt;script type="text/javascript"&gt;
          jQuery(document).ready(function($){
              $('#yt_resmenu_collapse .nav-menu &gt; li.parent').each(function(){
                  $(this).find('&gt; ul').wrap('&lt;div class="res-wrapnav"&gt;');
                  $(this).append('&lt;span class="menuress-toggle"&gt;&lt;/span&gt;');
              });
          });
          jQuery(window).load(function(){
              jQuery('#yt_resmenu_collapse .parent .menuress-toggle').css('height', jQuery('#yt_resmenu_collapse .parent &gt; a').outerHeight());
  
              jQuery('#yt_resmenu_collapse .parent &gt; .res-wrapnav').each(function(){
                  if(jQuery(this).parent().hasClass('open')){
                      jQuery(this).css('height', jQuery(this).children('ul').height());
                  }
              });
              jQuery('#yt_resmenu_collapse .parent .menuress-toggle').click(function(){
                  if(jQuery(this).parent().hasClass('open')){
                      jQuery(this).parent().removeClass('open');
                      jQuery(this).parent().children('.res-wrapnav').css('height', '0px');
                  }else{
                      jQuery(this).parent().addClass('open');
                      jQuery(this).parent().children('.res-wrapnav').css('height', jQuery(this).parent().children('.res-wrapnav').children('ul').height());
                  }
              });
          });
      &lt;/script&gt;
  &lt;/div&gt;
  &lt;?php
  }elseif($var_yttheme-&gt;getParam("responsive_menu")=='3'){ ?&gt;
  &lt;button type="button" class="btn btn-navbar"&gt;&lt;i class="icon-reorder"&gt;&lt;/i&gt;
  &lt;/button&gt;
  &lt;div id="yt_resmenu_sidebar"&gt;
      &lt;ul class="nav-menu clearfix"&gt;
      &lt;li &lt;?php echo $homecls;?&gt;&gt;
          &lt;a &lt;?php echo $homecls;?&gt; href="&lt;?php echo $this-&gt;getUrl('') ?&gt;"     title="Home"&gt;&lt;span&gt;Home&lt;/span&gt;&lt;/a&gt;
      &lt;/li&gt;
      &lt;?php foreach ($this-&gt;getStoreCategories() as $_category): ?&gt;
          &lt;?php echo $this-&gt;drawItem($_category) ?&gt;
      &lt;?php endforeach ?&gt;
      &lt;/ul&gt;
      &lt;script type="text/javascript"&gt;
          jQuery(document).ready(function($){
              $('body#bd').append('&lt;div class="yt_ressidebar_screennav"&gt;&lt;nav id="yt_screennav"&gt;&lt;ul class="siderbar-menu"&gt;&lt;/ul&gt;&lt;/nav&gt;&lt;/div&gt;');
              $('#yt_screennav ul.siderbar-menu').html($('#yt_resmenu_sidebar ul.nav-menu').html());
  
                  $('#yt-responsivemenu .btn.btn-navbar').click(function(){
                      if($('body#bd').hasClass('onpen-sidebar')){
                          $('body#bd').removeClass('onpen-sidebar');
                      }else{
                          $('body#bd').addClass('onpen-sidebar');
                      }
                  });
          });
      &lt;/script&gt;
  &lt;/div&gt;
  &lt;?php
  }else{
  $options = array();
  $category_collection = Mage::getModel('catalog/category')-&gt;getCollection();
  $category_collection-&gt;addAttributeToSelect('*');
  foreach ($this-&gt;getStoreCategories() as $_category){
      $catIds[] = $_category-&gt;getId();
  }
  $regexp  = "[0-9/]+(".implode("|", $catIds)."){1}[0-9/]?";
  if (Mage::helper('catalog/category_flat')-&gt;isEnabled()) {           
      $category_collection-&gt;addPathsFilter();
  }else{
      $category_collection-&gt;addPathFilter($regexp);
  }
  foreach($category_collection as $category){
      $c = new stdClass();
      $c-&gt;label = $category-&gt;getName();
      $c-&gt;value = $category-&gt;getId();
      $c-&gt;level = $category-&gt;getLevel();
      $c-&gt;parentid = $category-&gt;getParentId();
      $c-&gt;url_path = $category-&gt;getUrlPath();
      $c-&gt;is_active = false;
      if ($this-&gt;getCurrentCategory()) {
          if($c-&gt;value == array_pop($this-&gt;getCurrentCategory()-&gt;getPathIds())){
              $c-&gt;is_active = true;
          }
      }           
      $cats[$c-&gt;value] = $c;
  }
  
  foreach($cats as $id =&gt; $c){
      if (isset($cats[$c-&gt;parentid])){
          if (!isset($cats[$c-&gt;parentid]-&gt;child)){
              $cats[$c-&gt;parentid]-&gt;child = array();
          }
          $cats[$c-&gt;parentid]-&gt;child[] =&amp; $cats[$id];
      }
  }
  foreach($cats as $id =&gt; $c){
      if (!isset($cats[$c-&gt;parentid])){
          $stack = array($cats[$id]);
          while( count($stack)&gt;0 ){
              $opt = array_pop($stack);
              $option = array(
                  'label' =&gt; ($opt-&gt;level&gt;2 ? str_repeat('- - ', $opt-&gt;level-2) : '') . $opt-&gt;label,
                  'value' =&gt; $opt-&gt;value,
                  'url_path'  =&gt; $opt-&gt;url_path,
                  'is_active' =&gt; $opt-&gt;is_active,
              );
              array_push($options, $option);
              if (isset($opt-&gt;child) &amp;&amp; count($opt-&gt;child)){
                  foreach(array_reverse($opt-&gt;child) as $child){
                      array_push($stack, $child);
                  }
              }
          }
      }
  }
  unset($cats);
  ?&gt;
  &lt;?php 
  if ($var_yttheme-&gt;isHomepage()===FALSE) {
      $homecls='';
  } else {
      $homecls='selected="selected"';
  }
  ?&gt; 
  &lt;script type="text/javascript"&gt;
  function MobileRedirectUrl(){
      window.location.href =    document.getElementById("yt_resmenu_selectbox").value;
  }
  &lt;/script&gt;
  &lt;div class="menu-selectbox"&gt;
  &lt;i class="icon-reorder"&gt;&lt;/i&gt;
  &lt;select id="yt_resmenu_selectbox" onchange="MobileRedirectUrl(this);"&gt;
  &lt;option value="&lt;?php echo $this-&gt;getUrl(); ?&gt;" &lt;?php echo $homecls ?&gt; &gt;&lt;?php echo $this-&gt;__('Home') ?&gt;&lt;/option&gt;
  &lt;?php foreach ($options as $item): 
  if($item['url_path'] != '/root-catalog' &amp;&amp; $item['value'] != '1'){
  ?&gt;
      &lt;option &lt;?php echo ($item['is_active'])?"selected='selected'":"" ?&gt; value="&lt;?php echo ($item['url_path'])?$this-&gt;getBaseUrl().$item['url_path']: $item['value'] ?&gt;"&gt;&lt;?php echo $item['label'] ?&gt;&lt;/option&gt;
  &lt;?php 
  } endforeach ?&gt;
  &lt;/select&gt;
  &lt;/div&gt;
  &lt;?php
  }
  ?&gt;
  </code></pre>