Sorting an array of jobs by various criteria Below is my current code for sorting an array, "$jobs_list".

It's not very DRY and it's fugly. And there are several more similar case statements still to go in. How could I write it so my brain doesn't twitch to look at it?

switch ($sort) {
    case 'number':
        usort($jobs_list, function($a, $b){
            return ( (int) $a-&gt;number &gt; (int) $b-&gt;number );
        });
        break;
    case 'client':
        usort($jobs_list, function($a, $b){
            return strcmp($a-&gt;client_contact_last_name, $b-&gt;client_contact_last_name);
        });
        break;
    case 'name':
        usort($jobs_list, function($a, $b){
            return strcmp($a-&gt;name, $b-&gt;name);
        });
        break;
    case 'status':
        usort($jobs_list, function($a, $b){
            return ( (int) $a-&gt;status &gt; (int) $b-&gt;status );
        });
        break;
    case 'revenue':
        usort($jobs_list, function($a, $b){
            return ( (float) $a-&gt;base_revenue &gt; (float) $b-&gt;base_revenue );
        });
        break;
    case 'profit':
        usort($jobs_list, function($a, $b){
            return ( (float) $a-&gt;profit &gt; (float) $b-&gt;profit );
        });
        break;
    case 'margin':
        usort($jobs_list, function($a, $b){
            return ( (float) $a-&gt;margin &gt; (float) $b-&gt;margin );
        });
        break;

    default:
        # do nothing...
}
First, it may make sense to make sure that the field you're checking in $sort, is the same as the propertyname of the object in $jobs_list.

Also, it looks like there's a lot of identical sort functions; the only difference being that the property you're checking is different.

You could just combine the switch statements that have an identical sorting strategy (revenue, status, profit, margin, etc..) and turn the field into a variable, such as:

switch ($sort) {
    case 'number':
    case 'status':
    case 'profit':
    case 'margin':
        usort($jobs_list, function($a, $b) use ($sort) {
            return ( (float) $a-&gt;$sort &gt; (float) $b-&gt;$sort );
        });
        break;

}