<?php
    
//---------------------------------------------------------------------------------------
//Sort compare functions: will be called by the PHP usort function
//   It receives 2 elements.  Since this is a 2 dimensional array, it receives 2 arrays.
//   The job is to tell usort whether to switch the elements by returning 1,
//   or to keep them as is by returning -1 or 0
//---------------------------------------------------------------------------------------
function by_lname_fname($a, $b)         //receive 2 arrays
{
    if ($a['lname'] >  $b['lname']) return  1;      //if $a lname > $b lname -> switch
    if ($a['lname'] <  $b['lname']) return -1;      //if $a lname < $b lname -> no switch
    if ($a['lname'] == $b['lname'])                 //if $a lname = $b lname
    {
        if ($a['fname'] >  $b['fname']) return  1;  //if $a fname > $b fname -> switch      
        if ($a['fname'] <  $b['fname']) return -1;  //if $a fname < $b fname -> no switch
        if ($a['fname'] == $b['fname']) return  0;  //no switch
    }
}

//By lastname the firstname descending ----------------------------------------------------

function by_lname_fname_desc($a, $b)        //receive 2 arrays
{
    if ($a['lname'] >  $b['lname']) return  1;      //if $a lname > $b lname -> switch
    if ($a['lname'] <  $b['lname']) return -1;      //if $a lname < $b lname -> no switch
    if ($a['lname'] == $b['lname'])                 //if $a lname = $b lname
    {
        if ($a['fname'] >  $b['fname']) return  1;  //if $a fname > $b fname -> switch      
        if ($a['fname'] <  $b['fname']) return -1;  //if $a fname < $b fname -> no switch
        if ($a['fname'] == $b['fname']) return  0;  //no switch
    }
}  

//By lastname ignoring case ---------------------------------------------------------

function by_lname_ignore_case($a, $b)   //receive 2 arrays
{
    $lname1 = strtolower($a['lname']);        //convret to lower case
    $lname2 = strtolower($b['lname']);        //convret to lower case

    if ($lname1 >  $lname2) return  1;      //if $a lname > $b lname -> switch
    if ($lname1 <  $lname2) return -1;      //if $a lname < $b lname -> no switch
    if ($lname1 == $lname2) return  0;      //if $a lname = $b lname -> no switch
}

//By id (numerically) descending -----------------------------------------------------

function by_id_desc($a, $b)             //receive 2 arrays
{
    return ( $b['id'] - $a['id'] );             //if $b > $a return positive -> switch
} 

?>

<?php include "../include.php"; ?>      <!-- hyperlink to see the code -->