Adjust usort function to sort on one of 2 fields
<p>I have two types of array's fields that I want to sort according to:</p>

<p>1st is <strong>registration_date</strong> and second is <strong>preregistration_date</strong></p>

<p>and here is the print_r array output (stored in variable <strong>$unsorted_users</strong>):</p>

<pre><code>Array
(
    [0] =&gt; stdClass Object
        (
            [id] =&gt; 120
            [registration_date] =&gt; 2012-10-19 16:57:46
            [username] =&gt; Jeff
        )
    [1] =&gt; stdClass Object
        (
            [id] =&gt; 121
            [preregistration_date] =&gt; 2012-12-23 16:57:46

        )
    [2] =&gt; stdClass Object
        (
            [id] =&gt; 122
            [registration_date] =&gt; 2012-11-30 16:57:46
            [username] =&gt; Susan
        )

)
</code></pre>

<p>I want to order this array <strong>DESC</strong> by <strong>registration_date</strong> and <strong>preregistration_date</strong> so it looks like this:</p>

<p>Array
    (
         [0] => stdClass Object
            (
                [id] => 121
                [preregistration_date] => 2012-12-23 16:57:46</p>

<pre><code>        )
    [1] =&gt; stdClass Object
        (
            [id] =&gt; 122
            [registration_date] =&gt; 2012-11-30 16:57:46
            [username] =&gt; Susan

        )
    [2] =&gt; stdClass Object
        (
            [id] =&gt; 120
            [registration_date] =&gt; 2012-10-19 16:57:46
            [username] =&gt; Jeff

        )

)
</code></pre>

<p>And my usort function is:</p>

<pre><code>$sorted_users = usort($unsorted_users, function($a, $b) {
   return strcmp ($a-&gt;registration_date, $b-&gt;registration_date) ;
});
</code></pre>

<p>But, It is sorting just by <strong>registration_date</strong> and if there is no registration_date e.g.:</p>

<pre><code> [1] =&gt; stdClass Object
            (
                [id] =&gt; 121
                [preregistration_date] =&gt; 2012-12-23 16:57:46

            )
</code></pre>

<p>I get an error.</p>

<p>How to adjust my function so it is registration_date or preregistation_date sorting? </p>

<p>Btw. format of registration_date and preregistration_date is always the same and also both cannot be in one object. So, this doesn't need to be validated.</p>

<p>Thanks in advance.</p>
