import java.util.*;
class Emp implements Comparable
{
int id;
String name;
float sal;
Emp(int id,String name,float sal)
{
this.id=id;
this.name=name;
this.sal=sal;
}
public String toString()
{
return "["+this.id+","+this.name+","+this.sal+"]";
}
public int compareTo(Object obj)
{
Emp e=(Emp)obj;
int val=this.name.compareTo(e.name);
return val;
}
}
class Sorting
{
public static void main(String...alt)
{
Emp e1=new Emp(111,"Prashant",5000);
Emp e2=new Emp(222,"Ravi",2000);
Emp e3=new Emp(555,"Kalyani",3000);
Emp e4=new Emp(333,"Namita",4500);
Emp e5=new Emp(444,"Govinda",1500);
TreeSet ts=new TreeSet();
ts.add(e1);
ts.add(e2);
ts.add(e3);
ts.add(e4);
ts.add(e5);
System.out.println(ts);
}
}
class Emp implements Comparable
{
int id;
String name;
float sal;
Emp(int id,String name,float sal)
{
this.id=id;
this.name=name;
this.sal=sal;
}
public String toString()
{
return "["+this.id+","+this.name+","+this.sal+"]";
}
public int compareTo(Object obj)
{
Emp e=(Emp)obj;
int val=this.name.compareTo(e.name);
return val;
}
}
class Sorting
{
public static void main(String...alt)
{
Emp e1=new Emp(111,"Prashant",5000);
Emp e2=new Emp(222,"Ravi",2000);
Emp e3=new Emp(555,"Kalyani",3000);
Emp e4=new Emp(333,"Namita",4500);
Emp e5=new Emp(444,"Govinda",1500);
TreeSet ts=new TreeSet();
ts.add(e1);
ts.add(e2);
ts.add(e3);
ts.add(e4);
ts.add(e5);
System.out.println(ts);
}
}
0 Comment to "Java program to sort in ascending order of user defined Object(comapre by name)."
Post a Comment