Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I order the friends list returned from the new Facebook Graph API?

You can get a list of friends of an authenticated user with:

https://graph.facebook.com/me/friends

Anyone have any idea how to order the list by user name? Because it doesn't by default. There's nothing in the documentation.

like image 221
typeoneerror Avatar asked May 19 '10 01:05

typeoneerror


3 Answers

we do this in several apps just by sorting in javascript.

function sortByName(a, b) {
    var x = a.name.toLowerCase();
    var y = b.name.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

var _friend_data = null
function lazy_load_friend_data(uid) {
  if (_friend_data == null) {    
    FB.api('/me/friends', function(response) {        
        _friend_data = response.data.sort(sortByName);
      }
    )
  }
}
like image 87
mainsocial Avatar answered Oct 22 '22 07:10

mainsocial


Figured out a solution. Eventually, we'll probably be able to order graph results. For now, I'm just doing this (javascript). Assuming that I got "fb_uid" from my PHP session:

var friends = FB.Data.query("SELECT name, uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1={0}) ORDER BY name", parseInt(fb_uid));
friends.wait(function(rows){
    console.log(rows);
});
like image 41
typeoneerror Avatar answered Oct 22 '22 08:10

typeoneerror


I think the whole OpenGraph API is still in a bit of a transitional stage from FB Connect. In any case, I would just do a good old order-by query in FQL, which you can still use. I can't imagine it will be too hard to change once the open graph way of doing this gets established.

This very good tutorial shows you how to do it:

http://thinkdiff.net/facebook/php-sdk-graph-api-base-facebook-connect-tutorial/

$fql    =   "select name, hometown_location, sex, pic_square from user where uid=xxxxxxxxxxxxxxx";
$param  =   array(
        'method'     => 'fql.query',
        'query'      => $fql,
        'callback'   => ''
);
$fqlResult   =   $facebook->api($param);
like image 26
Bill Prin Avatar answered Oct 22 '22 06:10

Bill Prin