我读到的是使用比较器对ArrayLists进行分类,但在所有例子中,使用<代码>compare To ,根据一些研究,这种方法是用来衡量成绩的方法。
I wanted to sort an ArrayList of custom objects by one of their properties: a Date object
(getStartDay()
). Normally I compare them by item1.getStartDate().before(item2.getStartDate())
so I was wondering whether I could write something like:
public class CustomComparator {
public boolean compare(Object object1, Object object2) {
return object1.getStartDate().before(object2.getStartDate());
}
}
public class RandomName {
...
Collections.sort(Database.arrayList, new CustomComparator);
...
}