I have input like:
array
0 =>
array
'task_uid' => '10214'
'createdate' => '2012-04-18 09:47:40'
'task_id' => '92966'
1 =>
array
'task_uid' => '10214'
'createdate' => '2012-04-18 09:47:40'
'task_id' => '92967'
I must sorted it by createdate in stable way.
I create this function:
function compareTime($a, $b) {
$a_timestamp = strtotime($a["createdate"]);
$b_timestamp = strtotime($b["createdate"]);
if ($a_timestamp > $b_timestamp)
return -1;
else if ($a_timestamp < $b_timestamp)
return 1;
else
return 0;
//return strcmp($a["createdate"], $b["createdate"]);
}
and
usort($input, array( $this, "compareTime"));
It should sorted from the oldest to the the newest but this mixed data. Also if datacreate is same then not change position.
But if I use in compareTime:
return strcmp($a["createdate"], $b["createdate"]);
Then it change element from example (it is wrong for me). Can you give me answer how should I sorted input?
Outputs (oldest to newest)
Codepad demo
If you want to do the sort within a class, e.g. using a method of the class for the comparison, it can be done like this:
Outputs the same as the first example.