vote up 0 vote down star

Question:

Array
(
    [1257984000] => 2009-11-11 19:00:00
    [1258416000] => 2009-11-16 19:00:00
    [1258722000] => 2009-11-20 08:00:00
    [1258394400] => 2009-11-16 13:00:00
)

This is my array. I want to sort based on the array number( ie. 1257984000,1258416000 etc).

Coding Used:

function cmp($a, $b) {
   if ($a == $b) {
     return 0;
   }
  return ($a < $b) ? -1 : 1;
}
uasort($array, 'cmp');

It is not sorting properly. Whether my coding style is correct? Please help me to solve this issue.

Thanks.

flag

1 Answer

vote up 6 vote down

Use ksort to sort by key.

link|flag
uksort in his case, since he wants a comparison function. – JW 2 days ago
1  
@JW: He doesn’t need a comparison function since the default comparison of ksort behaves is the same as his. – Gumbo 2 days ago

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.