For object sorting
package comparator.sorting;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Animals implements Comparable<Animals> {
String name;
String breed;
String color;
public Animals() {
// TODO Auto-generated constructor stub
}
public Animals(String name, String breed, String color) {
this.name = name;
this.breed = breed;
this.color = color;
}
public String getName() {
return name;
}
public String getBreed() {
return breed;
}
public String getColor() {
return color;
}
public int compareTo(Animals o) {
// TODO Auto-generated method stub
return this.name.compareTo(o.getName());
}
public static void main(String[] args) {
List<Animals> animalList = new ArrayList<Animals>();
animalList.add(new Animals("Lion", "cat", "yellow"));
animalList.add(new Animals("tiger", "cat", "yellow"));
animalList.add(new Animals("puma", "cat", "back"));
Collections.sort(animalList);
for (int i = 0; i < animalList.size(); i++) {
System.out.println(animalList.get(i).getName());
}
}
}