What is Comparable and Comparator interface in Java?

Comparable Interface

A Comparable object is compare the object itself with another data. its implements java.lang.comparable interface.

Method syntax :-  Public int compareTo(object obj)

We should override this method in such a way that it returns a negative integer, zero, or a positive integer if “this” object is less than, equal to, or greater than the object passed as argument.

				
					//model class
public class Employee implements Comparable<Employee> {
	String name;
	int age;

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

	public int compareTo(Employee e1) {
		return name.compareTo(e1.name);
	}
}
				
			
				
					import java.util.ArrayList;
import java.util.Collections;

//main class
public class EmployeeMain {
	public static void main(String[] args) {
		
		ArrayList<Employee> a = new ArrayList<Employee>();
		a.add(new Employee("pravin", 23));
		a.add(new Employee("vijay", 63));
		a.add(new Employee("aman", 78));
		Collections.sort(a);
		for (Employee e : a) {
			System.out.println(e.name + "  " + e.age);
		}
	}
}
				
			

Output  :-
aman  78
pravin  23
vijay  63

In output Employee name is sorting in ascending order.

Comparator interface :-

Comparator interface can compare two different objects. its provide multiple sorting.
This interface is found in Java.util package.

Method Syntax :-  Public int compare(Object obj1, Object obj2)

Here first object is compare from second object.
As below example we can sort Employee class based on name, age.

				
					public class Employee {
	String name;
	int age;

	public Employee(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
}
				
			
				
					import java.util.Comparator;

//From this class We are compare name
public class NameComparator implements Comparator {
	public int compare(Object obj1, Object obj2) {
		
		Employee e1 = (Employee) obj1;
		Employee e2 = (Employee) obj2;
		return e1.name.compareTo(e2.name);
	}
}
				
			
				
					import java.util.Comparator;

//From this class we compare age
public class AgeComparator implements Comparator {
	
	public int compare(Object obj1, Object obj2) {
		Employee e1 = (Employee) obj1;
		Employee e2 = (Employee) obj2;
		if (e1.age == e2.age)
			return 0;
		else if (e1.age < e2.age)
			return -1;
		else
			return 1;
	}
}
				
			
				
					import java.util.ArrayList;
import java.util.Collections;

//Now I execute main class.
public class EmployeeMain {
	public static void main(String[] args) {
		
		ArrayList<Employee> a = new ArrayList<Employee>();
		a.add(new Employee("pravin", 23));
		a.add(new Employee("vijay", 63));
		a.add(new Employee("aman", 78));
		System.out.println("sorting by name –-");
		Collections.sort(a, new NameComparator());
		for (Employee e : a) {
			System.out.println(e.name + "  " + e.age);
		}
		System.out.println("sorting by age---");
		Collections.sort(a, new AgeComparator());
		for (Employee e : a) {
			System.out.println(e.name + "  " + e.age);
		}
	}
}
				
			

Output :-

sorting by name –-
aman 78
pravin 23
vijay 63

sorting by age –-
pravin 23
vijay 63
aman 78