我有一个水果模型类:
public class Fruit{
private String name;
private String color;
public Fruit(String name, color){
this.name = name;
this.color = color;
}
public getName(){
return name;
}
public getColor(){
return color;
}
}
然后,我创建了一个列表fruits
:
List<Fruit> fruits = new ArrayList<Fruit>();
fruits.add(new Fruit("Orange","orange"));
fruits.add(new Fruit("Strawberry","red"));
fruits.add(new Fruit("Apple","green"));
fruits.add(new Fruit("Banana","yellow"));
现在,我想根据水果名称按字母顺序fruits
对元素进行排序。如何有效地对水果列表进行排序?