项目中对ArrayList进行排序,采用如下方法:
class SortByNumber implements Comparator<ExchangeCommodity.ListBean> {
public int compare(ExchangeCommodity.ListBean listBean1, ExchangeCommodity.ListBean listBean2) {
if (listBean1.getNumber() > listBean2.getNumber()) {
return 1;
}else {
return 0;
// java1.7之后必须返回-1
}
}
}
Collections.sort(commodities, new SortByNumber()); // commodities是待排序的ArrayList