import java.util.*;
class Pet {
String name;
Pet(String name){
this.name=name;
}
public boolean equals(Object o){
return name.equals(((Pet)o).name);
}
public int hashCode(){
return name.length();
}
}
class myComp implements Comparator<Pet>{
public int compare(Pet p1, Pet p2){
return (((Pet)p2).name).compareTo(((Pet)p1).name);
}
}
class TestInts
{
public static void main(String[] args)
{
List<Pet> list=new ArrayList<Pet>();
list.add(new Pet("One"));
list.add(new Pet("Two"));
list.add(new Pet("Three"));
list.add(new Pet("Abc"));
myComp mp=new myComp();
Collections.sort(list,mp);
for(Pet p:list)
System.out.println("::"+p.name);
}
}
hope the above code will help