我有一个要按字母顺序排序的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();
}
最佳答案
将所有内容添加到列表后,您必须进行排序:
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());
}
});