title: Small sorting algorithm in php
body: <p>It should quite simple algorithm, but I just can't get around it.</p>
  
  <p>I have some arrays in alphabetical order</p>
  
  <pre><code>[0] =&gt; Array
      (
          [0] =&gt; a
          [1] =&gt; b
          [2] =&gt; c
      )
  </code></pre>
  
  <p>and for example</p>
  
  <pre><code>[0] =&gt; Array
      (
          [0] =&gt; a
          [1] =&gt; b
          [2] =&gt; c
          [3] =&gt; d
      )
  </code></pre>
  
  <p>and I need to sort them into rows. For example:</p>
  
  <p>I should receive a table with 3 columns and as many rows as it may get and it should be in alphabetical order.</p>
  
  <p>Here is an example:
  First array should be converted into</p>
  
  <pre><code>[0] =&gt; Array
      (
          [0] =&gt; Array
              (
                  [0] =&gt; a
                  [1] =&gt; b
                  [2] =&gt; c
              )
  
      )
  </code></pre>
  
  <p>But second one should be as</p>
  
  <pre><code>[1] =&gt; Array
      (
          [0] =&gt; Array
              (
                  [0] =&gt; a
                  [1] =&gt; c
                  [2] =&gt; d
              )
          [1] =&gt; Array
              (
                  [0] =&gt; b
              )
      )
  </code></pre>
  
  <p>I'm writing it in <code>php</code>, so if anyone can help I would be really appreciated.</p>
  
  <p><strong>UPD:</strong>
  Code example:</p>
  
  <pre><code>function sortAsOrdered( array $categories )
  {
      foreach ( $categories as $groupId =&gt; $group )
      {
          $regroupMenuItems = array();
          $limit            = count( $group );
          $rows             = ceil( $limit / 3 );
  
          for ( $i = 0; $i &lt; $rows; ++$i )
          {
              $jumper = 0;
  
              for ( $j = 0; $j &lt; 3; $j++ )
              {
                  if ( 0 == $jumper )
                  {
                      $jumper = $i;
                  }
  
                  if ( isset( $group[ $jumper ] ) )
                  {
                      $regroupMenuItems[ $i ][ $j ] = $group[ $jumper ];
                  }
  
                  $jumper = $jumper + $rows;
              }
          }
  
          $categories[ $groupId ] = $regroupMenuItems;
      }
  
      return $categories;
  }
  </code></pre>
  
  <p>Guys I solved this one. Here you could see my algorithm <a href="http://pastebin.com/xe2yjhYW" rel="nofollow">http://pastebin.com/xe2yjhYW</a>.
  But don't be sad your help will not go in vain. I probably will place bounty just for those who helped with this dificult algorithm for me.</p>
  
  <p>Guys thanks one more time. Your thoughts inspired me to think differently.</p>