soycharliente Posted July 29, 2009 Share Posted July 29, 2009 I'm trying to sort an array chronologically. After a bit of searching, the best way I can find it to use uasort() with a callback function. Can someone help me understand this example and how I could transform it to compare dates? The dates are in a multidimensional array. $feed[0]['release_date'] <?php // Comparison function function cmp($a, $b) { if ($a == $b) return 0; return ($a < $b) ? -1 : 1; } // Array to be sorted $array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4); print_r($array); // Sort and print the resulting array uasort($array, 'cmp'); print_r($array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/168009-solved-sort-array-chronologically/ Share on other sites More sharing options...
Psycho Posted July 29, 2009 Share Posted July 29, 2009 What is the format of the date? That would determine how you compare the dates, but assuming the dates are in the format yyyymmdd, then you could do this: function sortByreleaseDate($a, $b) { if ($a['release_date'] = $b['release_date']) { return 0; } return ($a['release_date'] < $b['release_date']) ? -1 : 1; } uasort($feed, 'sortByReleaseDate'); Quote Link to comment https://forums.phpfreaks.com/topic/168009-solved-sort-array-chronologically/#findComment-886129 Share on other sites More sharing options...
soycharliente Posted July 29, 2009 Author Share Posted July 29, 2009 It's stored as 2009-07-24 14:16:00 (I can easily concatenate the date and time together). And it's another dimension down in the array. Does that throw a wrench in the mix? Array ( [0] => Array ( [release_date] => 2009-07-24 [release_time] => 14:16:00 [display_start_date] => 2009-07-24 [display_stop_date] => 2015-12-31 [id] => 3179 [title] => Event Title [summary] => A one sentence summary about the event. [img_embed] => [img_alt] => ) [1] => Array ( [release_date] => 2009-07-23 [release_time] => 11:07:00 [display_start_date] => 2009-07-23 [display_stop_date] => 2009-09-01 [id] => 3172 [title] => Event Title [summary] => A one sentence summary about the event. [img_embed] => [img_alt] => ) [2] => Array ( [release_date] => 2009-07-09 [release_time] => 11:55:00 [display_start_date] => 2009-07-09 [display_stop_date] => 2009-11-09 [id] => 3130 [title] => Event Title [summary] => A one sentence summary about the event. [img_embed] => [img_alt] => ) ) Quote Link to comment https://forums.phpfreaks.com/topic/168009-solved-sort-array-chronologically/#findComment-886136 Share on other sites More sharing options...
soycharliente Posted July 29, 2009 Author Share Posted July 29, 2009 Buddy told me that $a and $b get passed associative arrays. Got it working now. Final solution: function cmp($a, $b) { if($a['display_start_date'] == $b['display_start_date']) { return 0; } return ($a['display_start_date'] > $b['display_start_date']) ? -1 : 1; } usort($feed, 'cmp'); Pretty close to what you had mjdamato (if not the exact same thing--it looks the same). I was messing with yours and showed them what you gave me. What I posted is what we eventually finished with. Quote Link to comment https://forums.phpfreaks.com/topic/168009-solved-sort-array-chronologically/#findComment-886153 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.