Make them all object of a type. either design a common Base class or an Interface
and then
use Comparator
to sort them out
For example.
public class SortableFields{
protected long rank;
//accessors methods
}
assumed that all the objects in arraylist are SortableFields now
Now
Collections.sort(list,new Comparator(){
public int compareTo(Object ob1,Object ob){
return ((SortableFild)ob1.getRank())-((SortableFild)ob2.getRank())
}
});
Or use reflection hack , not preferable
Collections.sort(list,new Comparator(){
public int compareTo(Object ob1,Object ob){
UtilClass.getRank(ob1)-UtilClass.getRank(ob);
}
});
In your UtilClass
public int getRank(Object ob){
Class cl=ob1.getClass();
Method mthd=cl.getMethod("getRank");
Integer output=(Integer)mthd1.invoke(ob);
return output;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…