[ create a new paste ] login | about

Link: http://codepad.org/skEiUkTC    [ raw code | output | fork ]

balajimca - PHP, pasted on Dec 2:
<?php
$array = array("2011-September_38","2011-June_4","2009-May_45","2010-November_9","2011-December_29","2010-March_19");
function yearCompare($a, $b) {
$yeara = strtolower(substr($a,0,4));
$yearb = strtolower(substr($b,0,4));
$year = range(2008,2100);
$year = array_flip($year);
    if($yeara == $yearb)
        return 0;
    if(!isset($year[$yeara]) || !isset($year[$yearb]))
    return $yeara > $yearb;
   return ($year[$yeara] > $year[$yearb]) ? 1 : -1;
}
function monthCompare($a, $b) {
$count = substr($a,strpos($a,'_'));
$count =strlen($count);
$count1 = substr($b,strpos($b,'_'));
$count1 =strlen($count1);
    $montha = strtolower(substr($a,5,-$count));
    $maonthb = strtolower(substr($b,5,-$count1));
    $months = array(
	'january'=> 1,
	'february'=> 2,
	'march'=>3,
	'april'=>4,
	'may'=>5,
	'june'=>6,
	'july'=>7,
	'august'=>8,
	'september'=>9,
	'october'=>10,
	'november'=>11,
	'december'=>12
 );
    if($montha == $monthb)
        return 0;
    if(!isset($months[$montha]) || !isset($months[$monthb]))
        return $montha > $monthb;
    return ($months[$montha] > $months[$monthb]) ? 1 : -1;

}
usort($array, "yearCompare");
//usort($array, "monthCompare");
foreach($array as $sorter)
{
echo $sorter."\n";
}
?>


Output:
1
2
3
4
5
6
2009-May_45
2010-March_19
2010-November_9
2011-September_38
2011-December_29
2011-June_4


Create a new paste based on this one


Comments: