Sunday, May 8, 2011

Sorting Facebook feeds in chronological order

College students spend lots of time on Facebook and virtually use it to share and organize their shcedules and course materials. Obviously, things get out of control and you end up with a very long and entangled feed that you might find yourself in a real need to explore or even worse, scan for something important.

So, one day a student of mine posted (!), wondering if there were any group settings that would arrange posts in reverse chronological order, presumably to save some time. That post caught my attention and eventually, I decided to prepare a bookmarklet for this exact purpose. It was pretty much straightforward but unfortunately, it would only work for groups. Someone may find it worthwhile to adapt it to the home/profile feeds, though.

Update: It seems the student got interested in this sort of JS magic and she actually took it a step further to work for the home feed as well.
// This snippet is licensed under a Creative Commons Attribution 3.0 License.
// Authors: Ahmed Abdelkader and Nancy Iskander
javascript:{
    var flist = document.getElementById('home_stream');
    if (flist == null)
    flist = document.getElementById('pagelet_group_mall').children[0].children[0];
    var far = new Array();
    var dates = {};
    while (flist.children.length) {
        var abbrs = flist.children[0].getElementsByTagName('abbr');
        if (abbrs.length) {
            dates[flist.children[0].id] = abbrs[0].getAttribute('data-utime');
            far.push(flist.children[0]);
        }
        flist.removeChild(flist.children[0]);
    }
    far.sort(function(a, b){return dates[b.id] - dates[a.id];});
    for (var i = 0; far.length - i; ++i)
        flist.appendChild(far[i]);
    void(0);
}
To create the bookmarklet, we just need to compress that and put it in a hyperlink. Just drag this link: Feed-Repairo!, into your browser's Bookmarks Toolbar to add the bookmark and you can easily rename it if you please. That's it! Whenever you need to sort out your group feeds, all you need to do is click the bookmark. Let me know if it works for you or you encoutner any bugs. Enjoy!

Update 8/23/2013: Drag this link if you want to sort ascendingly Repairo-Feed!
Update 3/23/2014: flist is not at the root container anymore; so we added another .children[0] and all is fine. (Many thanks to Jeff E)

Wednesday, January 26, 2011

A lightweight anonymous visitor for Java collections

I got myself into a situation where I really wished that Java collections accepted a visitor interface of some kind so I could define one on the fly and get my job done. Alas, I decided to implement the utility myself and here's the result.

public interface IVisitor<T> {
void visit(T item);
}

public class CollectionUtils {
public static<T> void applyVisitor(Collection<T> collection, IVisitor<T> visitor) {
for (T item : collection)
visitor.visit(item);
}
}
A typical usage example would be:
public static void main(String[] args) {
CollectionUtils.applyVisitor(Arrays.asList(1, 2, 3, 4, 5), new IVisitor<Integer>() {
public void visit(Integer x) {
System.out.println(x);
}
});
}