Drongo_III Posted February 21, 2013 Share Posted February 21, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274795-sorting-multidimensional-array-on-date-again/ Share on other sites More sharing options...
Jessica Posted February 21, 2013 Share Posted February 21, 2013 (edited) 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 February 21, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/274795-sorting-multidimensional-array-on-date-again/#findComment-1413995 Share on other sites More sharing options...
Barand Posted February 21, 2013 Share Posted February 21, 2013 As Jessica said, they are sorted. Those dates are 2013-01-04 2013-01-21 2013-01-24 2013-01-26 2013-12-19 2013-12-22 Quote Link to comment https://forums.phpfreaks.com/topic/274795-sorting-multidimensional-array-on-date-again/#findComment-1414015 Share on other sites More sharing options...
Drongo_III Posted February 21, 2013 Author Share Posted February 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274795-sorting-multidimensional-array-on-date-again/#findComment-1414019 Share on other sites More sharing options...
Jessica Posted February 21, 2013 Share Posted February 21, 2013 You can't see that those numbers are in ascending order?? Quote Link to comment https://forums.phpfreaks.com/topic/274795-sorting-multidimensional-array-on-date-again/#findComment-1414020 Share on other sites More sharing options...
kicken Posted February 21, 2013 Share Posted February 21, 2013 This may just be a typo when posting, but incase it isn't: function cmp ($a, $B) { return $a['date'] - $b['date']; } usort($socialMediaArray, "cmp"); // Sort the array by date order. You use $B in the parameter list, but $b in the function body. Quote Link to comment https://forums.phpfreaks.com/topic/274795-sorting-multidimensional-array-on-date-again/#findComment-1414022 Share on other sites More sharing options...
Barand Posted February 21, 2013 Share Posted February 21, 2013 @kicken Try posting $b followed by ) on this board without getting it converted to $B Quote Link to comment https://forums.phpfreaks.com/topic/274795-sorting-multidimensional-array-on-date-again/#findComment-1414026 Share on other sites More sharing options...
kicken Posted February 21, 2013 Share Posted February 21, 2013 Ah, is it yet another forum bug? Seems so. Though, disabling emoticons in the options fixes it. Quote Link to comment https://forums.phpfreaks.com/topic/274795-sorting-multidimensional-array-on-date-again/#findComment-1414027 Share on other sites More sharing options...
Barand Posted February 21, 2013 Share Posted February 21, 2013 Ah, is it yet another forum bug? Seems so. IMHO the whole editor on this board is one big bug Quote Link to comment https://forums.phpfreaks.com/topic/274795-sorting-multidimensional-array-on-date-again/#findComment-1414029 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.