Tuesday, January 10, 2012

BlackBerry Simple Sorting Vector


Creative Commons Licence

This work is licenced under a Creative Commons Licence.

Someone requested an example of using a SimpleSortingVector with the Facebook API. I don't know what a FriendsRequestObject looks like so I have assumed it has a getName() method that returns the name as a String.

import net.rim.device.api.util.Comparator;

/**
* A comparitor class to compare the class that defines the objects you want to sort
*/
public class MyComparator implements Comparitor {
  public int compare(Object o1, Object o2) {
    FriendsRequestObject f1 = (FriendsRequestObject)o1;
    FriendsRequestObject f2 = (FriendsRequestObject)o2;

    return f1.getName().compareTo(f2.getName());
  }

  public boolean equals(Object obj) {
    return compare(this, obj) == 0;
  }
}
import net.rim.device.api.util.SimpleSortingVector;

SimpleSortingVector ssv = new SimpleSortingVector();
ssv.setSortComparator(new MyComparator());

Profile[] f=user.getFriends();
for(int i=0;i < f.length; i++){
  String id=f[i].getId();
  String name=f[i].getName();
  vector.addElement(new FriendsRequestObject(id,name));
}

ssv.reSort();

No comments:

Post a Comment