I have an array of weekdays (see below) that I would like to sort as "Mon-Tue-Wed-Thu-Fri-Sat-Sun".
"Sun"=>59
"Sat"=>41
"Fri"=>21
"Thu"=>11
"Wed"=>14
"Tue"=>19
"Mon"=>31
I tried the following code but it doesn't seem to work correctly, the result being the ordered array pasted above, i.e. not in the order I would like it to be.
function orderbyweekday($a, $b) {
if (strcmp($a, "Mon") == 0)
$a = 0;
else if (strcmp($a, "Tue") == 0)
$a = 1;
else if (strcmp($a, "Wed") == 0)
$a = 2;
else if (strcmp($a, "Thu") == 0)
$a = 3;
else if (strcmp($a, "Fri") == 0)
$a = 4;
else if (strcmp($a, "Sat") == 0)
$a = 5;
else if (strcmp($a, "Sun") == 0)
$a = 6;
if (strcmp($b, "Mon") == 0)
$b = 0;
else if (strcmp($b, "Tue") == 0)
$b = 1;
else if (strcmp($b, "Wed") == 0)
$b = 2;
else if (strcmp($b, "Thu") == 0)
$b = 3;
else if (strcmp($b, "Fri") == 0)
$b = 4;
else if (strcmp($b, "Sat") == 0)
$b = 5;
else if (strcmp($b, "Sun") == 0)
$b = 6;
// if same day, return 0
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
uksort($movies_per_day, "orderbyweekday");
I would also like to do a similar thing with an array of month-years (e.g. "June 2010"=>10, "May 2009"=>111, etc.), but once I get this right it should be easier.
Thanks a lot.