Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array that looks like this

array(24) { ["#HiddenId"]=> string(24) "2013-11-08T11:59:54.378Z"] }

It has multiple ids/dates but I want to sort it by date (most recent)

I know how to do this for this format, "01/01/2014" however I Believe it is easier to work with this format, but I'm unsure of how to implement.

This is the code I have for format "01/01/2014"

uasort($fileList, "my_sort");

function my_sort($a,$b)
{
    $date1 = DateTime::createFromFormat('d/m/Y', $a);
    $date2 = DateTime::createFromFormat('d/m/Y', $b);
    return $date1 < $date2;
}

I need to do this function similar but for the other format shown above.

Any help is appreciated

share|improve this question
    
Am I missing something or can this be done with a simple arsort($yourArray) ?? –  Petar Zivkovic Apr 17 at 10:44
    
indeed thank you lol –  Joe Apr 17 at 10:52

1 Answer 1

up vote 2 down vote accepted

Very simple:

usort($yourArray,"strcmp");

"Big-endian" formats like Y-m-d H:i:s sort lexicographically.

share|improve this answer
    
How can I reference "hiddenId" without hardcoding like you have? –  Joe Apr 17 at 10:29
    
Isn't that part of your array structure? Don't you know the key of the date? –  Niet the Dark Absol Apr 17 at 10:30
    
@NiettheDarkAbsol Nice answer, but wouldn't rsort work in the same way, while being simpler to use? –  NorthBridge Apr 17 at 10:30
    
Well I need to sort the array by dates only, why do I need to reference the key values? –  Joe Apr 17 at 10:31
    
Because... you need to tell it where your date is? –  Niet the Dark Absol Apr 17 at 10:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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