title: Group and sort an array of objects based on 2 parameters
body: <p>I have an array of objects which I want to sort based on one property, and then kind of 'group together' based on another property. In the example below, I would like them sorted based on <code>$sequence</code>, and grouped based on <code>$artist</code>.</p>
  
  <p>// a stripped down version of my class :</p>
  
  <pre><code>&lt;?php
  class myClass
  {
      public $sequence;
      public $artist;
  
      function __construct($sequence,$artist)
      {
          $this-&gt;sequence=$sequence;
          $this-&gt;artist=$artist;
      }
  
      static function cmp_myclass_sequence($a, $b)
      {
          $a_seq = $a-&gt;sequence;
          $b_seq = $b-&gt;sequence;
          if ($a_seq == $b_seq) {
              return 0;
          }
          return ($a_seq &gt; $b_seq) ? +1 : -1;
      }
      static function cmp_myclass_artist($a, $b)
      {
          $a_art = strtolower($a-&gt;artist);
          $b_art = strtolower($b-&gt;artist);
          if ($a_art == $b_art) {
              return 0;
          }
          return ($a_art &gt; $b_art) ? +1 : -1;
      }
  }
  
  //some objects of my class which am using for testing :
  
  $myObj1 = new myClass(1,'A');
  $myObj2 = new myClass(1,'B');
  $myObj3 = new myClass(1,'C');
  $myObj4 = new myClass(2,'A');
  $myObj5 = new myClass(2,'B');
  $myObj6 = new myClass(2,'C');
  $myObj7 = new myClass(4,'A');
  $myObj8 = new myClass(5,'A');
  $myObj9 = new myClass(3,'B');
  $myObj10 = new myClass(3,'G');
  $myObj11= new myClass(3,'A');
  
  $myArr=array($myObj1,$myObj2,$myObj3,$myObj4,$myObj5,$myObj6,$myObj7,$myObj8,$myObj9,$myObj10,$myObj11);
  
  echo "My Array Before sort : &lt;br&gt;";
  foreach ($myArr as $obj){ print_r($obj); echo "&lt;br&gt;"; }
  
  usort($myArr, array("myClass", "cmp_myclass_sequence"));
  //usort($myArr, array("myClass", "cmp_myclass_artist"));
  
  
  echo "My Array AFTER sort : &lt;br&gt;";
  foreach ($myArr as $obj){ print_r($obj); echo "&lt;br&gt;";}
  
  ?&gt;
  </code></pre>
  
  <p>Both the sorting functions that I have in the class, <code>cmp_myclass_sequence</code> and <code>cmp_myclass_artist</code> work well on their own. But I am not sure how to get the array grouped in this way :
  All the A artists come first, then the B and so on.
  Amongst the A artists, the objects should be sorted by the sequence int of that object.</p>
  
  <p>In short this is the result that am looking for :</p>
  
  <pre><code>/*
  My Array AFTER sort : 
  
  myClass Object ( [sequence] =&gt; 1 [artist] =&gt; A )
  myClass Object ( [sequence] =&gt; 2 [artist] =&gt; A )
  myClass Object ( [sequence] =&gt; 3 [artist] =&gt; A )
  myClass Object ( [sequence] =&gt; 4 [artist] =&gt; A )
  myClass Object ( [sequence] =&gt; 5 [artist] =&gt; A )
  myClass Object ( [sequence] =&gt; 1 [artist] =&gt; B )
  myClass Object ( [sequence] =&gt; 2 [artist] =&gt; B )
  myClass Object ( [sequence] =&gt; 3 [artist] =&gt; B )
  myClass Object ( [sequence] =&gt; 1 [artist] =&gt; C )
  myClass Object ( [sequence] =&gt; 2 [artist] =&gt; C )
  myClass Object ( [sequence] =&gt; 3 [artist] =&gt; G ) 
  */
  </code></pre>