1

Possible Duplicate:
Sort an array of dates using usort() and sort() functions by the timestamp converted by mktime()

I'm trying to sort an array of dates using sort() and usort() only - without using mktime(). I'm trying to compare month, day and year, but can not get the correct result, plus it gives me a bunch of warnings. Will appreciate any help.

$dates = array ('10-10-2003', '2-17-2002', '2-16-2003','1-01-2005', '10-10-2004' );
function toTime($date) {

return sort ($date, SORT_STRING);
}

function sortByTime($a, $b) {
$a = toTime($a);
$b = toTime($b);
if($a == $b) {
    return 0;
}
return $a < $b ? -1 : 1 ;
}

usort($dates, 'sortByTime');
print_r($dates);

Thank you so much.

4

1 回答 1

1

使用uksort通过回调按键排序

在回调中只需将日期解析为时间戳并使用简单的比较

须藤代码:

function cmp($a, $b)
{
    global $array;
    return strcmp($array[$a]['db'], $array[$b]['db']);
}

uksort($array, 'cmp');
于 2012-11-18T20:16:39.617 回答