Plugin Post 2 Posts: How to list most connected posts?

I have done this way:

// Find connected pages (for all posts)
p2p_type( 'actors_movies' )->each_connected( $wp_query );

Now we have a $connected property attached to every post on the $wp_query, that is an array of posts connected by actors_movies.

$actors = array();
while (have_posts()) : the_post();
    $movies_acted = count($post->connected);
    if($movies_acted > 0) {
        $actors[$post->ID] = $movies_acted; 
    }
endwhile;
arsort($actors);
foreach ($actors as $id => $quantity) {
    //Show the actor info
}

Then, I have made an array with the ID of the $post as the key, and the quantity of $connected as the value, for each. Then I have ordered it by the value, and I have done a foreach through the array, to get the post from every ID, starting from the most prolific =D

tech