我有一个我想按字母顺序排序的ArrayList。有人可以帮助我吗?
我的代码是:
listProd = new ArrayList<Producer>();
Collections.sort(listProd, new Comparator<Producer>() {
@Override
public int compare(Producer p1, Producer p2) {
return p1.getLastname().compareToIgnoreCase(p2.getLastname());
}
});
if (!checked) {
for (Tour t : listTour) {
listProd.addAll(t.getProducers());
}
} else {
listProd = service.getAllProducers();
}
答案 0 :(得分:2)
您必须在之后对进行排序,并将所有内容添加到列表中:
if (!checked) {
listProd = new ArrayList();
for (Tour t : listTour) {
listProd.addAll(t.getProducers());
}
} else {
listProd = service.getAllProducers();
}
Collections.sort(listProd, new Comparator<Producer>() {
@Override
public int compare(Producer p1, Producer p2) {
return p1.getLastname().compareToIgnoreCase(p2.getLastname());
}
});