Let's say I have an object like:
public class Fruit{
private String name;
private int quantity;
Fruit(){}
Fruit(String name, int quantity){
this.name = name;
this.quantity= quantity;
}
public int getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
And I want to sort an array full of Fruit objects alphabetically by name. My initial thought, Arrays.sort(a.getName());
wouldn't work, because .getName()
only works on individual objects. One idea I had was put all the names into an array, sort those alphabetically, then run a loop to sort the objects using this list, but that seems absurdly cumbersome.
Any ideas? As you can tell, I'm very new to working with objects in this manner.