2

I have tried to use usort but I am having issues

I have now used the usort function.

my array is has a string key and a string value which represents the date

my output is as follows:

02/09/2013
03/09/2013
03/10/2013
04/07/2013
04/09/2013
09/09/2013
11/09/2013
13/06/2013
13/08/2013

It is only sorting by the first two numbers, I want it to sort for the full date, What am I doing wrong?

This is my code:

usort($filesWithDates,"my_sort");
foreach ($filesWithDates as &$value) {
    echo $value."<br/>";
}

function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
foreach ($filesWithDates as &$value) {
        echo $value."<br/>";
    }
1
  • You can avoid many issues if you use ISO format for dates (YYYY-mm-dd) or simple unix timestamps. Other than that, you have to parse/explode $a and $b in your compare function, not just compare the string. Commented Mar 4, 2014 at 15:20

2 Answers 2

4

Those aren't dates. Those are strings. You need to put your dates into a proper format that is either sortable as strings or compare them as dates using DateTime():

usort($filesWithDates, function($a, $b) {
    $date1 = DateTime::createFromFormat('d/m/Y', $a);
    $date2 = DateTime::createFromFormat('d/m/Y', $b);
    return $date1 > $date2;
});
foreach ($filesWithDates as $value) {
    echo $value."<br/>";
}
Sign up to request clarification or add additional context in comments.

5 Comments

Didn't know usort takes callable as parameter. Nice!
I have one problem, before i call this function, my key value is a string, after this function is executed, my array keys have been changed to '0, 1, 2, 3, 4....' etc. Why would it override my key ?
From the manual: Note: This function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned, rather than just reordering the keys. So if you want to preserve keys just copy the array to a new variable first and then sort that array.
Okay, but how I would I then re-match the keys with the reordered dateS?
Try changing usort to uasort
0

Convert your dates to integers first:

$datesAsInts = array();
foreach($filesWithDats as $value) {
    $datesAsInts[] = strtotime($value);
}
usort($datesAsInts,"my_sort");
foreach ($datesAsInts as $value) {
    echo date('d/m/Y', $value)."<br/>";
}

function my_sort($a,$b)
{
    if ($a==$b) return 0;
    return ($a<$b)?-1:1;
}

5 Comments

This still does not parse the dates and sort them correctly
@ToBe How about giving someone time to write their answer... When you down voted it was an exact copy of his code.
Ok, granted. But why post the answer if it wasnt finished? Anyways. Not the place to debate this. I'll remove my downvote when the answer changes. (Down removed)
@ToBe Why? Because if you don't post half an answer and then edit it these days then someone else will have pipped you to it. And even then half the time someone will still beat you to it.
Youre right, that happens a lot. Still I couldnt guess youre still editing. But dont worry, neg has been removed. So no need to chat about it anymore.

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.