Sunday, May 17, 2015

Writing our own Collection mechanism to Sort different objects in Java.

SortCollections.java
---------------------------------------
package com.ranga.collections;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortCollections<T> {

private List<T> collectionList;
private String sortingField = null;
private boolean isAscending = true;

public SortCollections(List<T> objectList, String sortField) {
this(objectList, sortField, true);
}

public SortCollections(List<T> collectionList, String sortField, boolean isAscending) {
super();
this.collectionList = collectionList;
this.sortingField = sortField;
this.isAscending = isAscending;
}

@SuppressWarnings("rawtypes")
public List<T> sort( final Class claz) {
final String sortingField = this.sortingField;
final Boolean isAscending = this.isAscending;

Collections.sort(this.collectionList, new Comparator<T>() {
@Override
public int compare(T object1, T object2) {
try {
Field sortField = claz.getDeclaredField(sortingField);
sortField.setAccessible(true);

Object value1 = sortField.get(object1);
Object value2 = sortField.get(object2);

if (value1 instanceof String && value2 instanceof String) { // String field
String string1 = (String) value1;
String string2 = (String) value2;
if (true == isAscending) {
return string1.compareTo(string2);
} else {
return string2.compareTo(string1);
}
} else { // Numeric field
Number number1 = (Number) value1;
Number number2 = (Number) value2;
if (true == isAscending) {
if (number1.floatValue() > number2.floatValue()) {
return 1;
} else {
return -1;
}
} else {
if (number2.floatValue() > number1.floatValue()) {
return 1;
} else {
return -1;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
});
return this.collectionList;
}
}

Employee.java
---------------------------------------
    package com.ranga.collections;
import java.io.Serializable;
public class Employee implements Serializable {

private int id;
private String name;
private int age;
private float salary;

public Employee() {
super();
}

public Employee(int id, String name, int age, float salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

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 float getSalary() {
return salary;
}

public void setSalary(float salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age
+ ", salary=" + salary + "]";
}
}

Application.java
---------------------------------------
package com.ranga.collections;
import java.util.ArrayList;
import java.util.List;
public class Application {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee(1,"Ranga", 26, 25677 ));
employees.add(new Employee(2,"Raja", 50, 2577 ));
employees.add(new Employee(3,"Vasu", 20, 30677 ));
employees.add(new Employee(4,"Mani", 45, 45677 ));
employees.add(new Employee(5,"Yasu", 29, 67677 ));
employees.add(new Employee(6,"Vinod", 80, 5677 ));

System.out.println(employees);
List<Employee> employeeList = new SortCollections<Employee>(employees, "name", false).sort(Employee.class);
System.out.println(employeeList);

employeeList = new SortCollections<Employee>(employees, "salary").sort(Employee.class);
System.out.println(employeeList);
}
}

0 comments: