1 实现一个Comparator 接口
2 比较类实现Comparable<TestObject>接口,并实现一个父类compareTo()方法
public class compater implements Comparator
{
public int compare(Object arg0, Object arg1)
{
TestObject o1 = null;
TestObject o2 = null;
int result = 0;
if(arg0 instanceof TestObject && arg1 instanceof TestObject){
o1 = (TestObject)arg0;
o2 = (TestObject)arg1;
}
result = o1.getAge() - o2.getAge();
return result>0?1:-1;
}
}
2 比较类实现Comparable<TestObject>接口,并实现一个父类compareTo()方法
public class TestObject implements Comparable<TestObject>
{
...
public int compareTo(TestObject arg0)
{
int result = 0;
TestObject o1 = null;
if(arg0 == null) return -1;
if(arg0 instanceof TestObject)
{
o1 = (TestObject)arg0;
result = (this.age-o1.getAge())>0?1:-1;
}
return result;
}
}