Monday, March 28, 2011

Data sorting by java Comparator,Comparable

package com.test;

public class Employee implements Comparable {


private int empId;
private String name;
private int age;

public Employee(int empId, String name, int age) {
this.empId = empId;
this.name = name;
this.age = age;
}
// getters & setters

public int getEmpId() {
return empId;
}

public void setEmpId(int empId) {
this.empId = empId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}


public int compareTo(Employee o) {
return this.empId - o.empId ;
// return this.age - o.age ;
}

}

---------------------------------------------------------------------
package com.test;

import java.util.*;

public class Util {

public static List getEmployees() {

List col = new ArrayList();

col.add(new Employee(5, "Frank", 28));
col.add(new Employee(1, "Jorge", 19));
col.add(new Employee(6, "Bill", 34));
col.add(new Employee(3, "Michel", 10));
col.add(new Employee(7, "Simpson", 8));
col.add(new Employee(4, "Clerk",16 ));
col.add(new Employee(8, "Lee", 40));
col.add(new Employee(2, "Mark", 30));

return col;
}
}

-----------------------------------------------------------------------
package com.test;

import java.util.*;

public class TestEmployeeSort {

@SuppressWarnings("unchecked")
public static void main(String[] args) {
List coll = Util.getEmployees();
// System.out.println(coll);
Collections.sort(coll); // sort method

//Collections.sort(coll, new EmpSortByName());
//Collections.sort(coll, new EmpSortByEmpId());
// Collections.sort(coll, new EmpSortByAge());
//System.out.println(coll);
printList(coll);
}

private static void printList(List list) {
System.out.println("EmpId\tName\tAge");
for (Employee e: list) {
System.out.println(e.getEmpId() + "\t" + e.getName() + "\t" + e.getAge());
}
}
}

-------------------------------------------------------------------
Result
EmpId Name Age
1 Jorge 19
2 Mark 30
3 Michel 10
4 Clerk 16
5 Frank 28
6 Bill 34

----------------------------------------------------------------------
package com.test;

import java.util.*;

public class EmpSortByName implements Comparator{

public int compare(Employee o1, Employee o2) {
return o1.getName().compareTo(o2.getName());
}
}
--------------------------------------------------
Result
EmpId Name Age
6 Bill 34
4 Clerk 16
5 Frank 28
1 Jorge 19
8 Lee 40
2 Mark 30
3 Michel 10
7 Simpson 8


---------------------------------------------------------
package com.test;

import java.util.Comparator;

public class EmpSortByAge implements Comparator{

public int compare(Employee o1, Employee o2) {
return o1.getAge() - o2.getAge();
}

}
-----------------------------------------------------------
Result

EmpId Name Age
7 Simpson 8
3 Michel 10
4 Clerk 16
1 Jorge 19
5 Frank 28
2 Mark 30
6 Bill 34
8 Lee 40

--------------------------------------------------------------
package com.test;

import java.util.*;

public class EmpSortByEmpId implements Comparator{

public int compare(Employee o1, Employee o2) {
return o1.getEmpId() - o2.getEmpId();
}

}
----------------------------------------------------------
Result
EmpId Name Age
1 Jorge 19
2 Mark 30
3 Michel 10
4 Clerk 16
5 Frank 28
6 Bill 34
7 Simpson 8
8 Lee 40
-----------------------------------------------------------------

No comments:

Post a Comment

Followers