Tuesday, December 20, 2011

How to implement MultyFieldComparable

Java.Util.Comparable is an interface used for sorting collections. In previous example, we used Comparable for sorting by only one field. In this example, we will see a more complex Comparable which can be used to sort a list of employees based on the name, then on department and then on age. The example consists of the following 2 classes.

Employee.java:

package com.soft.model;

public class Employee implements Comparable{
private String name;
private String department;
private int age;
public String getName() {
            return name;
}
public void setName(String name) {
            this.name = name;
}
public String getDepartment() {
            return department;
}
public void setDepartment(String department) {
            this.department = department;
}
public int getAge() {
            return age;
}
public void setAge(int age) {
            this.age = age;
}
@Override
public int compareTo(Object obj) {
            Employee employee=(Employee)obj;
            if(this.getName().compareTo(employee.getName())==0){
                        if(this.getDepartment().compareTo(employee.getDepartment())==0){
                                    return this.getAge()-employee.getAge();
                        }else{
                                    return this.getDepartment().compareTo(employee.getDepartment());
                        }
            }else{
                        return this.getName().compareTo(employee.getName());
            }
}

}



MultyFieldComparableExample.java:

package com.soft.examples;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.soft.model.Employee;

public class MultyFieldComparableExample {
            public static void main(String[] args) {
                        List<Employee> list= new ArrayList<Employee>();
                        Employee e=new Employee();
                        e.setName("Danial Fritz");
                        e.setDepartment("Field Based Development");
                        e.setAge(36);
                        list.add(e);
                        e=new Employee();
                        e.setName("Denis Khoo");
                        e.setDepartment("Project Management");
                        e.setAge(30);
                        list.add(e);
                        e=new Employee();
                        e.setName("Joe Kum");
                        e.setDepartment("Adminstration");
                        e.setAge(40);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(26);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(26);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(25);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Project Management");
                        e.setAge(26);
                        list.add(e);
                        System.out.println("************ Before Sorting ***********");
                        for (int i=0;i< list.size();i++){
                                    System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
                        }
                        System.out.println("************ Name, Department, Age Sorting through Comparable ***********");
                        Collections.sort(list);
                        for (int i=0;i< list.size();i++){
                                    System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
                        }
                       
            }
}


Output:

************ Before Sorting ***********
Danial Fritz   ----- Field Based Development   ----- 36
Denis Khoo     ----- Project Management        ----- 30
Joe Kum        ----- Adminstration             ----- 40
Anil Kumar     ----- Information Technology    ----- 26
Anil Kumar     ----- Information Technology    ----- 26
Anil Kumar     ----- Information Technology    ----- 25
Anil Kumar-----Project Management-----26

****** Name, Department, Age Sorting through Comparable *******

Anil Kumar     ----- Information Technology     ----- 25
Anil Kumar     ----- Information Technology     ----- 26
Anil Kumar     ----- Information Technology     ----- 26
Anil Kumar     ----- Project Management         ----- 26
Danial Fritz   ----- Field Based Development    ----- 36
Denis Khoo     ----- Project Management         ----- 30
Joe Kum        ----- Adminstration              ----- 40

An example of a MultyFieldComparator can be found at
An example of a simple Comparable can be found at
You may also find the following articles helpful.




_________________________________________________________________________

No comments:

Post a Comment