18

I have an array with keys as date in this format.

$arr = array(

    "20110805" => "2",
    "20100703" => "5",
    "20110413" => "3",
    "20100805" => "4",
    "20100728" => "6",
    "20090416" => "7",
    "20080424" => "8",
    "20110819" => "1",  
);

How can I sort this array by key?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
bharath
  • 1,233
  • 2
  • 11
  • 19
  • 1
    @TheHorse - There are lots of things which can be found by googling. It's much preferred to show duplicates on SO than LMGTFY-type comments. :) – Jared Farrish Aug 20 '11 at 21:33
  • 1
    @TheHorse - http://meta.stackexchange.com/questions/15650/ban-lmgtfy-let-me-google-that-for-you-links The purpose of SO is to be a repository of information, regardless of the simplicity or complexity of the question and/or answer, as long as it fits in the rules found in the FAQ. If it's a duplicate or answered elsewhere on SO, linking to those answers are considered good etiquette. – Jared Farrish Aug 20 '11 at 21:44

3 Answers3

35

With the dates in that format, an alphabetical comparison will work just fine. Use the PHP function ksort.

ksort($arr);
coyotebush
  • 643
  • 6
  • 10
  • hi thanks for your answer, ksort will sort the array by key but the key here are dates and ksort doesn't sort the array in order. i need to convert the keys to a valid date format and then do ksort, but doing that will affect the rest of the code. any suggestions?? – bharath Aug 20 '11 at 21:28
  • 1
    @bharath - Your dates are in YEAR MONTH DAY order, which means you can sort them using a normal sort and it will put it in proper order, as long as you have `0`'s preprended to the short MONTHs and DAYs. Your "dates" are essentially just a number that can be parsed into a date. – Jared Farrish Aug 20 '11 at 21:31
  • With the data you've shown us no extra manipulation is needed, and the example Jared posted works. – coyotebush Aug 20 '11 at 21:33
  • @Jared Farrish - thanks, but if you look at the example in codepad you can see the order is still not right. how can i get to to be in date order? – bharath Aug 20 '11 at 21:34
  • @Jared Farrish - i am sorry guys may be i am reading it wrong. looks like its good. thanks, i've just been reading it wrong all the time. – bharath Aug 20 '11 at 21:35
  • @bharath - Ok; it can be hard to read right at first in that format. Here's an example just in case: http://codepad.org/PYcR13C2 – Jared Farrish Aug 20 '11 at 21:40
  • @bharath - Also don't forget to mark an appropriate answer and add an up vote tp answer that are correct. – Jared Farrish Aug 20 '11 at 21:45
  • Just one notice, if you are trying to `return ksort($arr)`, it will return a `1`. You should execute the `ksort($arr)` over the array and then return it. – Gus Oct 02 '17 at 22:20
10

A slightly more complex solution, which nonetheless works for almost any date format, is based on the uksort function.

First we define a callback function that compares two dates (comparator):

function compare_date_keys($dt1, $dt2) {
    return strtotime($dt1) - strtotime($dt2);
}

Now we can use the just defined function as the second parameter in uksort, as in the example below:

uksort($arr, "compare_date_keys");

As a result the function will treat the key as a date and sort the array in ascending order (less recent first).

Note that we can easily tweak the comparator to support different use cases. For example, sorting by date descending (most recent first) can be accomplished by simply replacing the function's return expression with the following:

return strtotime($dt2) - strtotime($dt1);
Sal Borrelli
  • 2,201
  • 19
  • 19
  • 1
    Based on https://stackoverflow.com/a/40462935/1110760 you can simply do `return strtotime($a) - strtotime($b);`. From the [phpdocs](https://www.php.net/manual/en/function.uksort.php): "The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second." – Jelmer Jan 08 '21 at 15:31
  • 1
    Got catch @Jelmer, I have updated the answer to reflect your comment. – Sal Borrelli Jan 11 '21 at 11:02
8

Just this single line of code:

ksort($arr);
Marian Galik
  • 876
  • 8
  • 21