0

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?

4
  • 3
    Don't use strcmp() to compare numbers. It won't work correctly if the numbers have different numbers of digits. Commented Jun 19, 2014 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. Commented Jun 19, 2014 at 23:52
  • 1
    Never mind, those aren't numbers, they're dates formatted in YYYYMMDD format. Commented Jun 19, 2014 at 23:53
  • 3
    does rsort take a second parameter as a compare function? Isn't that typically reserved for usort functions. Commented Jun 19, 2014 at 23:53

2 Answers 2

1

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
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

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!
1

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.

Comments

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.