Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Thanks to @MarkBaker, @MajorCaiger and @hdvianna, here is the final order callback function that I am using.</p> <p>This function takes in to account multiple sort criteria from a multi-dimensional array.</p> <pre><code>/** Sort the data (if the sort key is defined) */ if(!empty($_REQUEST['orderby'])) : usort($invitees[$status], array(&amp;$this, '_order_callback')); endif; /** * Callback function to order event invitees * function _order_callback($item_a, $item_b){ /** Grab 'orderby', which must have been set for this function to be called */ $orderby = $_REQUEST['orderby']; /** If no 'order' is not set, default to ASC */ $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'ASC'; switch($orderby) : case 'ID' : // ID is unique, so just sort by ID $result = strnatcmp($item_a[$orderby], $item_b[$orderby]); break; case 'email' : $result = strnatcasecmp($item_a[$orderby], $item_b[$orderby]); if($result === 0) : $result = strcmp($item_a['first_name'], $item_b['first_name']); endif; if($result === 0) : $result = strcmp($item_a['surname'], $item_b['surname']); endif; break; case 'name' : $result = strcmp($item_a['first_name'], $item_b['first_name']); // Explicitly declare 'first_name' here as $orderby is actuualy 'name', which is a constructed field for display if($result === 0) : $result = strcmp($item_a['surname'], $item_b['surname']); endif; if($result === 0) : $result = strnatcasecmp($item_a['email'], $item_b['email']); endif; break; default : // 'custom' and 'town' $result = strcmp($item_a[$orderby], $item_b[$orderby]); break; endswitch; return (strtoupper($order) === 'ASC') ? $result : -$result; //Send final sort direction to usort } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload