package javaapplication1;
import java.util.ArrayList;
import java.util.Collections;
/**
*
* @author sowjanya.g
*/
public class Main {
public static void main(String[] args) {
ArrayList al=new ArrayList();
person p1=new person("sowjanya","sowji1.gutla@gmail.com","24");
person p2=new person("lavanya","ll.gutla@gmail.com","24");
person p3=new person("chaithu","chaithu1.gutla@gmail.com","24");
person p4=new person("avinash","zzzzi.moram@gmail.com","24");
al.add(p1);
al.add(p4);
al.add(p3);
al.add(p2);
System.out.println("al---before--"+al);
Collections.sort(al);
System.out.println("al----after---"+al);
}
}
class person implements Comparable
{
String name;
String mailId;
String age;
public int compareTo(Object o)
{
person per=(person)o;
return mailId.compareTo(per.getMailId());
}
person(String name1,String mail,String age1)
{
name=name1;
mailId=mail;
age=age1;
}
// method used to know the o/p as strings instead objects
@Override
public String toString()
{
return "Name: "+name+"---mailId--"+mailId+"---age--"+age;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getMailId() {
return mailId;
}
public void setMailId(String mailId) {
this.mailId = mailId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.Collections;
/**
*
* @author sowjanya.g
*/
public class Main {
public static void main(String[] args) {
ArrayList al=new ArrayList();
person p1=new person("sowjanya","sowji1.gutla@gmail.com","24");
person p2=new person("lavanya","ll.gutla@gmail.com","24");
person p3=new person("chaithu","chaithu1.gutla@gmail.com","24");
person p4=new person("avinash","zzzzi.moram@gmail.com","24");
al.add(p1);
al.add(p4);
al.add(p3);
al.add(p2);
System.out.println("al---before--"+al);
Collections.sort(al);
System.out.println("al----after---"+al);
}
}
class person implements Comparable
{
String name;
String mailId;
String age;
public int compareTo(Object o)
{
person per=(person)o;
return mailId.compareTo(per.getMailId());
}
person(String name1,String mail,String age1)
{
name=name1;
mailId=mail;
age=age1;
}
// method used to know the o/p as strings instead objects
@Override
public String toString()
{
return "Name: "+name+"---mailId--"+mailId+"---age--"+age;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getMailId() {
return mailId;
}
public void setMailId(String mailId) {
this.mailId = mailId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
No comments:
Post a Comment