public List<Bean> getList() {
List<Bean> beanList = new ArrayList<Bean>();
// some code here
while(hasNext()) {
// some code here
Bean bean = new Bean();
listing(bean, beanList);
}
// sort by age bean here before returning
return beanList
}
public void listing(Bean bean, List<Bean> beanList) {
//some code here
bean.setName(var1);
bean.setAddress(var2);
bean.setAge(var3);
beanList.add(bean)
}
我需要根据年龄对arraylist进行排序。如何对基于bean的数组列表进行排序?
我尝试过使用收集器,但我不确定如何使用这些代码来实现它。
谢谢。
是比较功能?另外,我可以使用'long compare'吗? – user2772219
不,它应该是'int compare()',它在'Comparator'接口中被定义为这样。我编辑了我的答案,包括完整的实施 – Thilak
明白了。我会尝试这个并回复你。谢谢 – user2772219