




Fonte : recebido por email, de uns amigos
public class MyObj {
private int myValue;
priate String desc;
public MyObj() {}
public void setMyValue( int value ) { this.myValue = value; }
public int getMyValue() { return this.myValue; }
public void setDesc( String newDesc ) { this.desc = newDesc; }
public String getDesc() { return this.desc; }
}
import java.util.Comparator;
public class MyObjComparator implements Comparator {
public int compare(Object o1, Object o2) {
MyObj ob1 = (MyObj)o1;
MyObj ob2 = (MyObj)o2;
if( ob1.getMyValue() == ob2.getMyValue() ) {
return 0;
} else if( ob1.getMyValue() < ob2.getMyValue() ) {
return -1;
} else if( ob1.getMyValue() > ob2.getMyValue() ) {
return 1;
}
return 0;
}
}
Collections.sort( myObjList, new MyObjComparator() );