opencodez

Java Sorting using Comparator

Lets see how we can sort our Employee objects using different criteria like id, name, salary.

SortTest remains the main class that will perform creating and filling objects for us. This time employee class is plain old java classs that holds some data about each employee we create.

Here I have created scenarios or you can say strategies OR classes for sorting as SortById,  SortByName, SortBySalary. Each of above class implements comparator interface and overrides compare method, which takes two objects to compare and return eithre -1, 1 or 0.

Note: I have added few helper functions that really helps while coding like  print x this takes array list of objects and prints in format I want instead of class id and all that. toString x  overridden this function so I can return the class string as required

Here goes the code x

import java.util.*;

public class SortTest
{
	public static void main(String args[])
	{
		new SortTest();
	}

	public SortTest()
	{
		//fill some employee objects
		ArrayList list = new ArrayList();
		list.add(new Employee(500,"Shifoo",150000));
		list.add(new Employee(504,"Oogway",120000));
		list.add(new Employee(503,"Tigress",100000));
		list.add(new Employee(730,"Mantis",45000));

		System.out.println("Initial List :");
		print(list);
		System.out.println("n");

		Collections.sort(list,new SortyById());
		System.out.println("Sorted List By Id:");
		print(list);
		System.out.println("n");

		Collections.sort(list,new SortyByName());
		System.out.println("Sorted List By Name:");
		print(list);
		System.out.println("n");

		Collections.sort(list,new SortyBySalary());
		System.out.println("Sorted List By Salary:");
		print(list);
		System.out.println("n");

	}

	public void print(ArrayList list)
	{
		Iterator it = list.iterator();
		while(it.hasNext())
		{
			Employee emp = (Employee) it.next();
			System.out.println(emp);
		}
	}
}

class Employee
{
	public int id;
	public String name;
	public double salary;

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

	public String toString()
	{
		return this.id +", "+this.name+", "+this.salary;
	}
}

//scenario | strategy - I
class SortyById implements Comparator
{
	public int compare(Object object1, Object object2)
	{
		int value=0;

		Employee emp1 = (Employee) object1;
		Employee emp2 = (Employee) object2;

		if(emp1.id x emp2.id)
			value = 1;
		else if(emp1.id x emp2.id)
			value = -1;
		else if(emp1.id == emp2.id)
			value = 0;

		return value;
	}
}

//scenario | strategy - II
class SortyByName implements Comparator
{
	public int compare(Object object1, Object object2)
	{
		Employee emp1 = (Employee) object1;
		Employee emp2 = (Employee) object2;

		return emp1.name.compareTo(emp2.name);
	}
}

//scenario | strategy - III
class SortyBySalary implements Comparator
{
	public int compare(Object object1, Object object2)
	{
		int value=0;

		Employee emp1 = (Employee) object1;
		Employee emp2 = (Employee) object2;

		if(emp1.salary x emp2.salary)
			value = 1;
		else if(emp1.salary x emp2.salary)
			value = -1;
		else if(emp1.salary == emp2.salary)
			value = 0;

		return value;
	}
}

Output:

Initial List :
500, Shifoo, 150000.0
504, Oogway, 120000.0
503, Tigress, 100000.0
730, Mantis, 45000.0


Sorted List By Id:
500, Shifoo, 150000.0
503, Tigress, 100000.0
504, Oogway, 120000.0
730, Mantis, 45000.0


Sorted List By Name:
730, Mantis, 45000.0
504, Oogway, 120000.0
500, Shifoo, 150000.0
503, Tigress, 100000.0


Sorted List By Salary:
730, Mantis, 45000.0
503, Tigress, 100000.0
504, Oogway, 120000.0
500, Shifoo, 150000.0

As you can see x