My Automobil类看起来像这样
public class AutoMobil {
private String make;
private String brandName;
private int yearOfManufacture;
}
我的autoMobil填充列表如下所示;
private void autoMobileList() {
MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Corolla’’, ‘’2011’’));
MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Corolla’’, ‘’2002’’));
MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Mark2’’, ‘’1998’’));
MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Camry’’, ‘’2004’’));
MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Corona’’, ‘’2011’’));
MyAutoMobilList.add(new AutoMobil(“Nissan’’, ‘’March’’, ‘’2013’’));
MyAutoMobilList.add(new AutoMobil(“Nissan’’, ‘’Micra’’, ‘’2001’’));
MyAutoMobilList.add(new AutoMobil(“Nissan’’, ‘’Laurel’’, ‘’2014’’));
MyAutoMobilList.add(new AutoMobil(“Mercedes’’, ‘’SL500’’, ‘’2004’’));
MyAutoMobilList.add(new AutoMobil(“Mercedes’’, ‘’190E’’, ‘’1992’’));
MyAutoMobilList.add(new AutoMobil(“Mercedes’’, ‘’C200’’, ‘’1993’’));
MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Hiace’’, ‘’1998’’));
MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Hiace’’, ‘’2003’’));
MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Vitz’’, ‘’2004’’));
}
答案 0 :(得分:1)
请参阅此link。我以前回答过。您的要求与此类似。
答案 1 :(得分:0)
按品牌名称排序(String
):
Collections.sort(list, new Comparator<AutoMobil>() {
public int compare(AutoMobil v1, AutoMobil v2) {
return v1.getBrandName().compareTo(v2.getBrandName());
}
});
按制造年份排序(int
):
Collections.sort(conversations, new Comparator<AutoMobil>(){
public int compare(AutoMobil s1, AutoMobil s2) {
if (s1.getYearOfManufacture() < s2.getYearOfManufacture())
return 1;
if (s1.getYearOfManufacture() > s2.getYearOfManufacture())
return -1;
else
return 0;
}
});
仅显示特定项目:
ArrayList<AutoMobil> list_to_show = new ArrayList<AutoMobil>();
for (int i = 0; i < list.size(); i++)
if (list.getBrandName().equals("Nissan"))
list_to_show.add(list.get(i));