Saturday, April 16, 2011

Facebook: Sort people by number of mutual friends

The Facebook's Find-Friends page lists people, whom Facebook thinks we may know. There are some ways to filter people by hometown or college (and etc...), but it lacks the simple ability to sort people by the number of mutual friends. Using Firefox and Greasemonkey add-on and the following script it can be done easily.
Add new user script in the Greasemonkey:
Add new user script.
Fill up the following information:
Fill-up.
Then copy the following script and past it to the newly opened sortby.user.js file and save it.
// ==UserScript==
// @name SortBy
// @namespace http://autosysprogs.blogspot.com/
// @description Sort by most mutual friends
// @include http://www.facebook.com/find-friends/*
// @author Mkrtich Soghomonyan
// ==/UserScript==
window.addEventListener(
'load',
function() {
// ----------------------------------------
// ----------------------------------------
// Add sort function
var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.innerHTML ='function sortByNum(a, b) { return b.num - a.num; } \
function sortPeopleByMostMutualFriends() { \
var allDivs, thisDiv, firstDiv, changeDiv; \
allDivs = document.evaluate("//div[@class=\'friendBrowserUnit\']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); \
var people = new Array(); \
for (var j = 0; j < allDivs.snapshotLength; j++) { \
var mutual = allDivs.snapshotItem(j).innerHTML.replace(/([0-9]*) mutual friend.*/g, "===========$1").replace(/^.*===========/, ""); \
if (!mutual.match(/^[0-9]*$/)) { mutual = 0; }\
people.push({num: mutual, val: allDivs.snapshotItem(j).cloneNode(true)}); \
} \
people.sort(sortByNum); \
var parent = new Array(); \
for (var i = 0; i < allDivs.snapshotLength; i++) { \
thisDiv = allDivs.snapshotItem(i); \
changeDiv = document.getElementById(thisDiv.id); \
if (changeDiv) { \
parent.push(changeDiv.parentNode); \
changeDiv.parentNode.removeChild(changeDiv); \
} \
} \
for (var i = 0; i < allDivs.snapshotLength; i++) { \
parent[i].appendChild(people[i].val);\
} \
}';
document.getElementsByTagName("head")[0].appendChild(scriptElement);
// ----------------------------------------
// ----------------------------------------
// Add sort button above the hometown check boxes.
// Get the hometown table.
var targetDiv = document.getElementById('checkboxes_hometown');
if (null == targetDiv) {
// Current page is different from find-friends page.
return;
}
// Create a div to surround the sort button.
var newDiv = document.createElement('div');
newDiv.setAttribute('id', 'sortPeopleByMostMutualFriends');
// Create the button and set its attributes.
var sortButton = document.createElement('input');
sortButton.name = 'sortButton';
sortButton.type = 'button';
sortButton.value = 'Sort by mutual friends';
sortButton.setAttribute("onclick", "sortPeopleByMostMutualFriends();");
// Append the button to the div.
newDiv.appendChild(sortButton);
// Append before hometown table.
targetDiv.parentNode.insertBefore(newDiv, targetDiv);
},
true);
view raw sortby.user.js hosted with ❤ by GitHub

In the Facebook's Find Friends page now you can see a button near the hometown filter.
Click it and it will sort people by number of mutual friends.

That's all.

3 comments:

  1. For Chrome you add:

    // @match http://facebook.com/*
    // @match http://*.facebook.com/*
    // @match https://facebook.com/*
    // @match https://*.facebook.com/*

    But then.. it only "sorts" by first 2 results.

    ReplyDelete
  2. That would be a great script but it doesn't work right now as facebook changed in late july the way the persons appear on the page. Could you be so kind to update it, i think it will be very very useful.

    Thank you,
    Babis

    ReplyDelete
  3. awesome dude, i gotta message you sometime i just joined this website but us fellow computer geeks gotta stick together!!!!!!! i will definately be in touch

    Later
    Billy maize

    ReplyDelete