Page 1 of 1

sorting according to User_defined Criteria

Posted: Tue Sep 01, 2009 10:08 pm
by Hphp
I am new in php , I read the book and Google, but still don't understand
How sorting according to User_defined Criteria work,
foe example what the following try to do

Code: Select all

<?php
$dates = array('10-10-2003', '2-17-2002', '2-16-2003', '1-01-2005', '10-10-2004');
sort($dates); 
// Array ( [0] => 10-01-2002 [1] => 10-10-2003 [2] => 2-16-2003 [3] => 8-18-2002 )
 
natsort($dates); 
// Array ( [2] => 2-16-2003 [3] => 8-18-2002 [1] => 10-01-2002 [0] => 10-10-2003 )
 
function DateSort($a, $b) {
 
    // If the dates are equal, do nothing.
    if($a == $b) return 0;
    
    // Dissassemble dates
    list($amonth, $aday, $ayear) = explode('-',$a);
    list($bmonth, $bday, $byear) = explode('-',$b);
 
    // Pad the month with a leading zero if leading number not present
    $amonth = str_pad($amonth, 2, "0", STR_PAD_LEFT);
    $bmonth = str_pad($bmonth, 2, "0", STR_PAD_LEFT);
 
    // Pad the day with a leading zero if leading number not present
    $aday = str_pad($aday, 2, "0", STR_PAD_LEFT);
    $bday = str_pad($bday, 2, "0", STR_PAD_LEFT);
 
    // Reassemble dates
    $a = $ayear . $amonth . $aday;
    $b = $byear . $bmonth . $bday;
 
    // Determine whether date $a > $date b
    return ($a > $b) ? 1 : -1;
}
 
usort($dates, 'DateSort'); 
 
print_r($dates);
?> :?: