Comparable
import java.util.*;
class A implements Comparable
{
int
no;
String
name;
A(int
a,String n)
{
this.no=a;
this.name=n;
}
int
getVal()
{
return
this.no;
}
String
getName()
{
return
this.name;
}
public
String toString()
{
return
this.no+" - "+this.name;
}
public
int compareTo(Object a1)
{
//return
this.no-((A)a1).no; -- Sort Number
return
(((A)a1).name).compareTo(this.name); // Sort Name
}
public
static void main(String []s)
{
A a[] = new A[5];
a[0]
= new A(3,"Vijay");
a[1]
= new A(4,"Ajay");
a[2]
= new A(1,"Sanjay");
a[3]
= new A(2,"Jay");
a[4]
= new A(5,"Parajay");
Arrays.sort(a);
for(int
i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}
import java.util.*;
class Ano implements Comparator
{
public
int compare(Object o1,Object o2)
{
return
((A)o1).no-((A)o2).no;
}
}
class Aname implements Comparator
{
public
int compare(Object o1,Object o2)
{
return
(((A)o1).name).compareTo(((A)o2).name);
}
}
class A
{
int
no;
String
name;
A(int
a,String n)
{
this.no=a;
this.name=n;
}
public
String toString()
{
return
this.no+" - "+this.name;
}
public
static void main(String []s)
{
ArrayList
a = new ArrayList();
a.add(new
A(3,"Vijay"));
a.add(new
A(4,"Ajay"));
a.add(new
A(1,"Sanjay"));
a.add(new
A(2,"Jay"));
a.add(new
A(5,"Parajay"));
Collections.sort(a,new
Aname());
Iterator
ir = a.iterator();
A
a1;
while(ir.hasNext())
{
a1
= (A)ir.next();
System.out.println(a1);
}
}
}