一、Comparator
强行对某个对象collection进行整体排序的比较函数,可以将Comparator传递给Collections.sort或Arrays.sort。接口方法:
public class SalesItemComparatorByFirstLetter implements Comparator<SalesItem> { @Override public int compare(SalesItem lhs, SalesItem rhs) { return lhs.getFirstLetter().compareTo(rhs.getFirstLetter()); } }SalesItem类里面存储有字母,这里根据字母来进行排序
@Override public int compareTo(Person o) { return this.age.compareTo(o.getAge()); }
原文地址:http://blog.csdn.net/fanxl10/article/details/41745731