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.

Here's the example array, $SocialPosts:

Array(

[18] => SocialPost Object
    (
        [time] => 20140415
        [url] => http://www.twitter.com/twitterapi
        [copy] => We have agreed to acquire @gnip, welcome to the flock! https://t.co/fXrE36fjPZ
        [image] => 
    )

[19] => SocialPost Object
    (
        [time] => 20140409
        [url] => http://www.twitter.com/twitterapi
        [copy] => RT @twittersecurity: http://t.co/OOBCosuKND & http://t.co/oPmJvpbS6v servers were not affected by OpenSSL vulnerability CVE-2014-0160 http:…
        [image] => 
    )

[20] => SocialPost Object
    (
        [time] => 20140602
        [url] => http://www.facebook.com/19292868552
        [copy] => Our June 2014 events calendar is up! Join us at events around the world this month, and learn how to build, grow, and monetize your apps with Facebook: https://developers.facebook.com/blog/post/2014/06/02/june-2014-developer-events-for-facebook-and-parse/
        [image] => https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBMV0YW8BCmCBMB&w=154&h=154&url=https%3A%2F%2Ffbstatic-a.akamaihd.net%2Frsrc.php%2Fv2%2Fy6%2Fr%2FYQEGe6GxI_M.png
    )

)

I'm creating each SocialPost Object by using data from their respective APIs (Twitter, Facebook), and then adding each SocialPost Object to a SocialPosts array.

I tried the following rsort so that I could list them all together in reverse order by the [time] property:

function cmp($a, $b){
    return strcmp($a->time, $b->time);
}

rsort($socialPosts, "cmp");

However, it's oddly enough sorting the Twitter posts in the right order, followed by the Facebook posts in the right order, in what looks like two separate sorts. It should order them together regardless source in the correct order according to the [time] value.

It's also worth nothing that after adding each SocialPost to the array, I run a loop to format and then update the [time] property correctly, since Twitter and Facebook provided the time detail in different formats. Here's that snippet as well:

foreach($socialPosts as $socialPost){
    $date_string = $socialPost->time;

    $date = new DateTime($date_string);
    $date->setTimezone(new DateTimeZone('America/New_York'));
    $formatted_date = $date->format('Ymd');

    $socialPost->time = $formatted_date;        
}

Any ideas as to what could be going wrong?

share|improve this question
3  
Don't use strcmp() to compare numbers. It won't work correctly if the numbers have different numbers of digits. –  Barmar Jun 19 at 23:51
    
This example in the PHP manual should help you. Also for reference, here's a good list of sorting methods to help you decide which function to use. –  scrowler Jun 19 at 23:52
1  
Never mind, those aren't numbers, they're dates formatted in YYYYMMDD format. –  Barmar Jun 19 at 23:53
3  
does rsort take a second parameter as a compare function? Isn't that typically reserved for usort functions. –  Jonathan Kuhn Jun 19 at 23:53
add comment

2 Answers

up vote 1 down vote accepted

You should look into usort()

    function cmp($a, $b) {
        return strtotime($a->time) - strtotime($b->time);
    }

    usort($ar, "cmp");

if you want to reverse the orders, just change the cmp function to:

return strtotime($b->time) - strtotime($a->time);

Which yields a return of:

Array
(
    [0] => stdClass Object
        (
            [time] => 20140409
            [url] => http://www.twitter.com/twitterapi
            [copy] => RT @twittersecurity: http://t.co/OOBCosuKND & http://t.co/oPmJvpbS6v servers were not affected by OpenSSL vulnerability CVE-2014-0160 http:…
            [image] => 
        )

    [1] => stdClass Object
        (
            [time] => 20140415
            [url] => http://www.twitter.com/twitterapi
            [copy] => We have agreed to acquire @gnip, welcome to the flock! https://t.co/fXrE36fjPZ
            [image] => 
        )

    [2] => stdClass Object
        (
            [time] => 20140602
            [url] => http://www.facebook.com/19292868552
            [copy] => Our June 2014 events calendar is up! Join us at events around the world this month, and learn how to build, grow, and monetize your apps with Facebook: https://developers.facebook.com/blog/post/2014/06/02/june-2014-developer-events-for-facebook-and-parse/
            [image] => https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBMV0YW8BCmCBMB&w=154&h=154&url=https%3A%2F%2Ffbstatic-a.akamaihd.net%2Frsrc.php%2Fv2%2Fy6%2Fr%2FYQEGe6GxI_M.png
        )

)
share|improve this answer
    
Awesome, you nailed it. I really didn't read too far into rsort() outside of "[t]his function sorts an array in reverse order (highest to lowest)." I figured, "reverse order," yeah that's what I need. I'm leaving out the strtotime() bit though, I need to find a more elegant manner of handling the difference in time formats, maybe a method after creating the $socialPost object. Thanks! –  Angel Marino Jun 20 at 14:55
    
@AngelMarino No worries mate, glad I could help :) –  Darren Jun 21 at 15:05
add comment

rsort doesn't take a comparison function. To sort with a user-defined comparison function, you have to use usort:

usort($socialPost, "cmp");

If you want to reverse the sort order, change the comparison function so it inverts its result.

share|improve this answer
add comment

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.