1

What is the fastest way of sorting this both alphabetically by country and numerically by date?:

Array
(
[JAPAN] => Array
    (
        [2010-10-17] => 2
    )

[CUBA] => Array
    (
        [2010-10-16] => 9
    )

[RUSSIAN FEDERATION] => Array
    (
        [2010-10-16] => 26
        [2010-10-17] => 6
        [2010-10-18] => 2
    )

[CHINA] => Array
    (
        [2010-10-16] => 13
    )

)
1
  • 2
    Why do you need the fastest way? Commented Oct 19, 2010 at 11:56

2 Answers 2

1
foreach ($array as $value) {

    ksort($value);
}

ksort($array);

http://codepad.org/wJn0hJN4

array(4) {
  ["CHINA"]=>
  array(1) {
    ["2010-10-16"]=>
    int(13)
  }
  ["CUBA"]=>
  array(1) {
    ["2010-10-16"]=>
    int(9)
  }
  ["JAPAN"]=>
  array(1) {
    ["2010-10-17"]=>
    int(2)
  }
  ["RUSSIAN FEDERATION"]=>
  array(3) {
    ["2010-10-16"]=>
    int(26)
    ["2010-10-17"]=>
    int(6)
    ["2010-10-18"]=>
    int(2)
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'd rather use the shorter code : foreach($array as &$value) { ksort($value); } ksort($array);.
0

You will have to benchmark on more data. I'd try ksort for sorting by countries and usort for sorting by dates.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.