0

I've read a few different topics on this (like this one and the general doc info about uasort() - which I believe is what I'm looking for), but the array is still being a bit stubborn (in my use-case).

Here's what I have:

// This sets an array of values to a variable
$collection_rows = get_field('collection_profiles');

This is the print_r of $collection_rows:

Array
(
    [0] => Array
        (
            [collection_profile] => Array
                (
                    [0] => stdClass Object
                        (
                            [ID] => 273
                            [post_author] => 1
                            [post_date] => 2012-03-26 07:53:45
                            [post_date_gmt] => 2012-03-26 13:53:45
                            [post_content] => 
                            [post_title] => Profile 1
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => closed
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => profile-1
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2012-04-12 08:07:35
                            [post_modified_gmt] => 2012-04-12 14:07:35
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => http://domain.com/?post_type=moulding_profiles&p=273
                            [menu_order] => 0
                            [post_type] => moulding_profiles
                            [post_mime_type] => 
                            [comment_count] => 0
                            [ancestors] => Array
                                (
                                )

                            [filter] => raw
                        )

                )

            [collection_profile_note] => 1
        )

    [1] => Array
        (
            [collection_profile] => Array
                (
                    [0] => stdClass Object
                        (
                            [ID] => 188
                            [post_author] => 1
                            [post_date] => 2012-02-17 15:24:24
                            [post_date_gmt] => 2012-02-17 21:24:24
                            [post_content] => 
                            [post_title] => Test Profile
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => closed
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => test-profile
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2012-02-28 14:13:32
                            [post_modified_gmt] => 2012-02-28 20:13:32
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => http://domain.com/?post_type=moulding_profiles&p=188
                            [menu_order] => 0
                            [post_type] => moulding_profiles
                            [post_mime_type] => 
                            [comment_count] => 0
                            [ancestors] => Array
                                (
                                )

                            [filter] => raw
                        )

                )

            [collection_profile_note] => 3
        )

    [2] => Array
        (
            [collection_profile] => Array
                (
                    [0] => stdClass Object
                        (
                            [ID] => 207
                            [post_author] => 1
                            [post_date] => 2012-02-23 13:35:55
                            [post_date_gmt] => 2012-02-23 19:35:55
                            [post_content] => 
                            [post_title] => Casing Test Profile
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => closed
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => casing-test-profile
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2012-02-23 13:35:55
                            [post_modified_gmt] => 2012-02-23 19:35:55
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => http://domain.com/?post_type=moulding_profiles&p=207
                            [menu_order] => 0
                            [post_type] => moulding_profiles
                            [post_mime_type] => 
                            [comment_count] => 0
                            [ancestors] => Array
                                (
                                )

                            [filter] => raw
                        )

                )

            [collection_profile_note] => 2
        )

)

(quite a doozey)

I'm looking to sort by an array key/value of collection_profile_note. What I've tried (up to this point) is:

$collection_rows = get_field('collection_profiles');
print_r($collection_rows);
if ($collection_rows) {
    echo '<h2>'.__('Profiles in Collection','roots').'</h2>';
    echo '<ul>';
    function cmp($a, $b) {
        if ($a->collection_profile_note == $b->collection_profile_note) {
            return 0;
        } else {
            return $a->collection_profile_note < $b->collection_profile_note ? 1 : -1;
        }
    }
    usort($collection_rows, 'cmp');

    foreach($collection_rows as $collection_row) {
        // Extract single post value
        $collection_profile_field = $collection_row['collection_profile'];
        $collection_profile_page = isset($collection_profile_field[0]) ? $collection_profile_field[0]->ID : NULL;
        ?>
        <li><a href="<?php echo get_permalink($collection_profile_page); ?>"><?php echo get_the_title($collection_profile_page); ?></a> <?php echo $collection_row['collection_profile_note']; ?></li>
    <?php }
    echo '</ul>';
}

and while it does change the order from what it displays without uasort() it doesn't order them how I'd like --> with function (2, 3, 1), without function (1, 3, 2)

Any help would be greatly appreciated. Thanks!

Community
  • 1
  • 1
Zach
  • 1,185
  • 3
  • 24
  • 60
  • 1
    Please copy the object array from your browser's "view source" instead of from the screen as displayed. That will maintain all the linebreaks and whitespace we need to be able to see what the heck is in that block of madness. – Michael Berkowski Apr 16 '12 at 20:28

2 Answers2

0

Well, you've implemented your sorting logic backwards:

function cmp($a, $b) {
    if ($a->collection_profile_note == $b->collection_profile_note) {
        return 0;
    } else {
        return $a->collection_profile_note < $b->collection_profile_note ? 1 : -1;
    }
}

From the documentation for uasort():

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

Note that you're using this logic:

  • If a < b, return 1 (in essence, telling the sorting function that a > b)
  • If a > b, return -1 (in essence, telling the sorting function that a < b)
FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
  • Thanks for the input - I updated the sorting logic - but the actual `foreach` statement (or `print_r()` for that matter) is still not outputting the arrays with the `collection_profile_note` in order (ascending). Made a quick Pastebin of this: http://pastebin.com/XJV9fMMr Thanks! – Zach Apr 17 '12 at 13:17
  • @Zach: Your sort logic is still backwards in your pastebin. – FtDRbwLXw6 Apr 17 '12 at 13:45
  • Ah, k that last part is now `return $a->collection_profile_note < $b->collection_profile_note ? -1 : 1;` but still no dice... – Zach Apr 17 '12 at 13:57
  • 1
    @Zach: Your array structure also seems to be "an array of arrays" instead of "an array of objects". You will need to modify your `cmp()` function to reflect this. The notation `$a->collection_profile_note` assumes that `$a` is an object, when it's actually an array according to your `print_r()` output. You should instead be comparing `$a['collection_profile_note']` and likewise for `$b`. – FtDRbwLXw6 Apr 17 '12 at 14:42
  • That was it! Thanks - figures why that implementation wasn't working ;) – Zach Apr 17 '12 at 14:48
0

Is it necessary for you to sort the array in place? It might be easier to break out the keys and values you're sorting on, sort those, then loop through that array (like so):

$sortme=array();
foreach( $collection_rows as $key=>$profile )
{
    $sortme[$key] = $profile['collection_profile_note'];
}

asort($sortme);

foreach( $sortme as $node=>$ignore ) 
{
    print_r($collection_rows[$node]);
}

Acknowledged this almost doubles the sorting time, but maybe enough for your needs?

gadhra
  • 21
  • 1