public void testComparatorSortName(){
Collections.sort(list,new Comparator () {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Emp && o2 instanceof Emp){
Emp e1 = (Emp) o1;
Emp e2 = (Emp) o2;
return e1.getName().compareTo(e2.getName());
}
throw new ClassCastException("不能转换为Emp类型");
}
});
System.out.println("使用Comparator比较器按name升序排序后:");
for(Object o : list){
System.out.println(o);
}
}