Sorting using Comparable Interface

Sorting using Comparable Interface

The Comparable interface is used by the Collection.sort() method and the java.util.Array.sort() method to sort Lists and array of objects, respectively. To implement Comparable, a class must implement a single method, compareTo(). 

Here’s an invocation of compareTo():
int x = thisObject.compareTo(anotherObject)

The compareTo() method returns an int with following characteristics :

-> negative if this object < anotherObject
-> zero if this object == anotherObject
-> postive if this object > anotherObject

The sort() method uses compareTo() to determine how the List or object array should be sorted. Since we got to implement compareTo() for our own classes, we can use whatever we prefere, to sort instances for our own classes.

Let have example that shows sorting using Comparable

package com.c2s.collection;

public class Employee  implements Comparable<Employee>{

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

	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
	public long getSalary() {
		return salary;
	}

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

	public int compareTo(Employee emp) {
		int compareId = ((Employee)emp).getId();
		return this.id-compareId;
	}

	@Override
	//this is overriden to print the user friendly information about the Employee
	public String toString() {
		return "[id=" + this.id + ", name=" + this.name + ", age=" + this.age + ", salary=" +this.salary + "]\n";
	}

}

package com.c2s.collection;

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



public class sortEmployee {
	
	public static void main(String[] args) {

		//sorting custom object array
		Employee[] empArr = new Employee[4];
		empArr[0] = new Employee(5, "Mikey", 25, 10000);
		empArr[1] = new Employee(4, "Arun", 29, 20000);
		empArr[2] = new Employee(10, "Lisa", 35, 5000);
		empArr[3] = new Employee(8, "Pankaj", 32, 50000);

		//sorting employees array using Comparable interface implementation
		Arrays.sort(empArr);
		System.out.println("Sorted List by Id:\n"+Arrays.toString(empArr));

		List<Employee> ll = new ArrayList<Employee>();

		List<Employee> names = Arrays.asList(empArr);
		Collections.sort(names);
		System.out.println("Sorted List by Name: "+names);

	}

}

Output:

Sorted List by Id:
[[id=4, name=Arun, age=29, salary=20000]
, [id=5, name=Mikey, age=25, salary=10000]
, [id=8, name=Pankaj, age=32, salary=50000]
, [id=10, name=Lisa, age=35, salary=5000]
]
Sorted List by Name: [[id=4, name=Arun, age=29, salary=20000]
, [id=5, name=Mikey, age=25, salary=10000]
, [id=8, name=Pankaj, age=32, salary=50000]
, [id=10, name=Lisa, age=35, salary=5000]
]

 

 

Sorting using Comparator interface

Leave a Reply

Your email address will not be published. Required fields are marked *