0

Test

asked 2011-04-06 01:06:55 +0800

perfomance gravatar image perfomance
51

Employee Class

import java.util.Date;

public class Employee {
private String firstName;
private String lastName;
private int age;
private Date dob;

public Employee(){
}
/**
* @param firstName
* @param lastName
* @param age
* @param dob
*/
public Employee(String firstName, String lastName, int age, Date dob) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.dob = dob;
}
/**
* @return Returns the firstName.
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName The firstName to set.
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return Returns the lastName.
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName The lastName to set.
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return Returns the age.
*/
public int getAge() {
return age;
}
/**
* @param age The age to set.
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return Returns the dob.
*/
public Date getDob() {
return dob;
}
/**
* @return dob The dob to set.
*/
public void setDob(Date dob) {
this.dob = dob;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Employee [FirstName=" + firstName + ", LastName=" + lastName
+ ", Age=" + age + ", Dob=" + dob + "]";
}
}



Test Class

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try{
ArrayList<Employee> objArrayList = new ArrayList<Employee>();
DateFormat objDateFormat = new SimpleDateFormat("dd-MMM-yy");
objArrayList.add(new Employee("Ashish","",26,objDateFormat.parse("27-May-1984")));
objArrayList.add(new Employee("Anjali","",24,objDateFormat.parse("22-Jan-1950")));
objArrayList.add(new Employee("Saurabh","",23,objDateFormat.parse("23-Jun-1983")));
objArrayList.add(new Employee("Sanket","",24,objDateFormat.parse("24-March-1980")));
objArrayList.add(new Employee("Nikhil","",22,objDateFormat.parse("20-Feb-1982")));
objArrayList.add(new Employee("Paresh","",27,objDateFormat.parse("19-Dec-1985")));
objArrayList.add(new Employee("Raj","",30,objDateFormat.parse("26-May-1980")));
objArrayList.add(new Employee("Kuldeep","",20,objDateFormat.parse("20-May-1999")));
objArrayList.add(new Employee("Arti","",21,objDateFormat.parse("21-May-1990")));
objArrayList.add(new Employee("Neha","",29,objDateFormat.parse("29-Jul-1981")));

Collections.sort(objArrayList, new Comparator(){
public int compare(Object o1, Object o2) {
Employee objEmployee1 = (Employee) o1;
Employee objEmployee2 = (Employee) o2;
return objEmployee1.getFirstName().compareToIgnoreCase(objEmployee2.getFirstName());
}

});
System.out.println("*************Sort by FirstName*************************");
int intSize = objArrayList.size();
for (int index = 0; index < intSize; index++) {
System.out.println("Employee:["+index+"]:FirstName>"+((Employee)objArrayList.get(index)).getFirstName());
}

Collections.sort(objArrayList, new Comparator(){
public int compare(Object o1, Object o2) {
int emp1Age = ((Employee)o1).getAge();
int emp2Age = ((Employee)o2).getAge();
if(emp1Age > emp2Age)
return 1;
else if(emp1Age < emp2Age)
return -1;
else
return 0;
}
});
System.out.println("\n*************Sort by Age*************************");
intSize = objArrayList.size();
for (int index = 0; index < intSize; index++) {
System.out.println("Employee:["+index+"]:FirstName>"+((Employee)objArrayList.get(index)).getFirstName()+" :Age> "+((Employee)objArrayList.get(index)).getAge());
}

Collections.sort(objArrayList, new Comparator(){
public int compare(Object o1, Object o2) {
Date emp1DOB = ((Employee)o1).getDob();
Date emp2DOB = ((Employee)o2).getDob();
return emp1DOB.compareTo(emp2DOB);
}
});
System.out.println("\n*************Sort by DOB*************************");
intSize = objArrayList.size();
for (int index = 0; index < intSize; index++) {
System.out.println("Employee:["+index+"]:FirstName>"+((Employee)objArrayList.get(index)).getFirstName()+" :Age> "+((Employee)objArrayList.get(index)).getAge()+" :DOB> "+((Employee)objArrayList.get(index)).getDob());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

}

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2011-04-06 03:39:18 +0800

Steva77 gravatar image Steva77 flag of Italy
1014 3
http://www.research.softe...

...and?

link publish delete flag offensive edit

answered 2011-04-06 09:27:10 +0800

Steva77 gravatar image Steva77 flag of Italy
1014 3
http://www.research.softe...

I meant: which is the question?

link publish delete flag offensive edit

answered 2011-04-07 00:05:59 +0800

RyanWu gravatar image RyanWu
533 2
about.me/flyworld

@perfomance : what happen about your code ?

if this is only testing post, i will move to to Test Category

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-04-06 01:06:55 +0800

Seen: 125 times

Last updated: Apr 07 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More