import java.util.ArrayList;
import java.util.Collections;
public class Animal implements Comparable<Animal> {
private String name;
public Animal(String name) {
this.name = name;
}
public String toString() {
return name;
}
@Override
public int compareTo(Animal that) {
// compare the two animal objects by name
return this.name.compareTo(that.name);
}
public static void main(String[] args) {
ArrayList<Animal> animals = new ArrayList<Animal>();
animals.add(new Animal("Zed"));
animals.add(new Animal("Diesel"));
animals.add(new Animal("Garfield"));
System.out.println("Animals before sort: ");
System.out.println(animals);
Collections.sort(animals);
System.out.println("Animals after sort: ");
System.out.println(animals);
}
}