Jump to content

Sorting multidimensional array on date - again


Drongo_III

Recommended Posts

Hi Guys

 

Posted on here a few days ago about sorting a multi dimensional array based on date. I thought I had it working but now i realise it doesn't appear to be sorting correctly.

 

Essentially I have an array that is combined from feeds from twitter and facebook. I converted the dates using strtotime and the unsorted array looks like this:

 


print_r($socialMediaArray);
(
   [0] => Array
    (
	    [date] => 1359214851
	    [message] => some message
	    [from] => twitter
    )
   [1] => Array
    (
	    [date] => 1358991499
	    [message] => some message
	    [from] => twitter
    )
   [2] => Array
    (
	    [date] => 1358799273
	    [message] => some message
	    [from] => twitter
    )
   [3] => Array
    (
	    [date] => 1387741833
	    [message] => some message
	    [from] => twitter
    )
   [4] => Array
    (
	    [date] => 1387490401
	    [message] => some message
	    [from] => twitter
    )
   [5] => Array
    (
	    [date] => 1357263343
	    [message] => some message
	    [from] => facebook
    )
)

 

I then ran the following code to try and sort on date order

 

function cmp ($a, $B)
 {
 return $a['date'] - $b['date'];
 }
 usort($socialMediaArray, "cmp"); // Sort the array by date order.


 echo "SORTED ARRAY <pre>";
 print_r($socialMediaArray);


SORTED ARRAY

Array
(
   [0] => Array
    (
	    [date] => 1357263343
	    [message] => some message
	    [from] => facebook
    )

   [1] => Array
    (
	    [date] => 1358799273
	    [message] => some message
	    [from] => twitter
    )

   [2] => Array
    (
	    [date] => 1358991499
	    [message] => some message
	    [from] => twitter
    )

   [3] => Array
    (
	    [date] => 1359214851
	    [message] => some message
	    [from] => twitter
    )

   [4] => Array
    (
	    [date] => 1387490401
	    [message] => some message
	    [from] => twitter
    )

   [5] => Array
    (
	    [date] => 1387741833
	    [message] => some message
	    [from] => twitter
    )

)


 

But when I then convert the dates back into something readable it becomes apparent that the dates haven't been sorted correctly as they come out as

 

Fri Jan 04 1:35:43

Mon Jan 21 20:14:33

Thu Jan 24 1:38:19

Sat Jan 26 15:40:51

Thu Dec 19 22:00:01

Thu Dec 19 22:00:01

 

 

I'm not overly familiar with using usort, hence why I posted here, so if someone could offer up a possible reason as to why this may be happening it would appreciated.

 

Many thanks,

 

Drongo

Link to comment
Share on other sites

You can easily look at those timestamps and see that they are IN FACT in order. 

1357....

13587...

13589...

1359....

13874...

13877...

 

So...the problem is clearly not in the code you've posted. 

 

Edit: The last one in the list is:

2013-12-22 19:50:33

and the first is:

2013-01-04 01:35:43

 

I think you need to refer to date() for the right formats to use for your human-readable date.

 

Edited by Jessica
Link to comment
Share on other sites

Thanks Jessica

 

You give me too much credit and I can't in fact see from the time stamps what the dates are... However you did give me cause to check what I was doing with the dates and it appears it wasn't the output that was the problem but there was some test code messing up the strtotime conversion earlier in the code. So thank you for prompting me to check that.

 

You can easily look at those timestamps and see that they are IN FACT in order.

1357....

13587...

13589...

1359....

13874...

13877...

So...the problem is clearly not in the code you've posted.

 

 

 

Edit: The last one in the list is:2013-12-22 19:50:33

and the first is:

2013-01-04 01:35:43

 

I think you need to refer to date() for the right formats to use for your human-readable date.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.