有一个类模型CountryModel。它有 2 个字段:id和title.
public class CountryModel {
private int id;
private String title;
public CountryModel(int id, String title) {
this.id = id;
this.title = title;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
}
我创建了一个models类型列表CountryModel来显示国家列表。
public CountryModelsAdapter() {
List<CountryModel> models = new ArrayList<>();
models.add(new CountryModel(TYPE_RUSSIA, "Россия"));
models.add(new CountryModel(TYPE_GERMANY, "Германия"));
models.add(new CountryModel(TYPE_FRANCE, "Франция"));
models.add(new CountryModel(TYPE_ITALY, "Италия"));
setModels(models);
}
如何models按字段对列表进行排序,title以便屏幕上的国家按字母顺序显示(德国-意大利-俄罗斯-法国)?
通过 Comparable 接口:
您还可以使用比较器
String在流上按字母顺序按类型字段排序的工作表的输出:国家型号:
型号(无变化):
溪流:
String使用sort类方法输出按类型字段按字母顺序排序的工作表ArrayList:国家型号:
模型排序:
Arrays.sort:(作为零食)