Sorting using Comparable basis of id and name

In this post, we will see Sorting using Comparable basis of id and name example in java.

Basic points about the comparable interface.

Comparable is an interface available in java.lang package, which provides single sorting(means we can sort either the basis of id or name at a time. Consider we have Employee class which contains fields name and id).

The Comparable interface contains compare() method. If we want to sort any collection of user-defined objects using comparable, our java class must need to implement Comparable interface and override compareTo(Object o) method.

Program to sort on the basis of the name using Comparable.

  • Define an Employee class which should implement Comparable interface and create two variables name and id.  Provide corresponding getter/setter and parameterized constructor. Here we are going to sort employee object on basis of Id.
  • Override compareTo () method and provide logic.

@Override
public int compareTo(Object o) {
    Employee employee = (Employee) o;
    String name = employee.name;
    return this.name.compareTo(name);
}

  • Create a couple of employee object using parameterized constructor and add into list.

List<Employee> emplist = new ArrayList<Employee>();
emplist.add(new Employee("John", 101));
emplist.add(new Employee("Eric", 106));
emplist.add(new Employee("Arya", 103));
emplist.add(new Employee("Sansa", 105));
emplist.add(new Employee("Cersei", 102));

  • Use Collections.sort(emplist) the method which will internally call the compareTo() method(Check here how String compareTo() method internally works in java). Here Collections is a class and sort() is a static method. Use Java 8 forEach() method and print the name and id.

Collections.sort(emplist);
emplist.forEach(empolyee-> System.out.println(empolyee.getName() + " " + empolyee.getId()));

Let’s see Complete example.

package sortingusingcomparable;

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

class Employee implements Comparable {
	private String name;
	private int id;

	public Employee(String string, int i) {
		this.name = string;
		this.id = i;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

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

	@Override
	public int compareTo(Object o) {
		Employee employee = (Employee) o;
		String name = employee.name;
		return this.name.compareTo(name);
	}
}

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

		List<Employee> emplist = new ArrayList<Employee>();
		emplist.add(new Employee("John", 101));
		emplist.add(new Employee("Eric", 106));
		emplist.add(new Employee("Arya", 103));
		emplist.add(new Employee("Sansa", 105));
		emplist.add(new Employee("Cersei", 102));

		Collections.sort(emplist);
		emplist.forEach(empolyee -> System.out.println(empolyee.getName() + "   " + empolyee.getId()));
	}
}

The output of the above program.

Arya 103
Cersei 102
Eric 106
John 101
Sansa 105

 

Program to sort on the basis of the id using Comparable.

For sorting on basis of id we need to change compareTo() logic as below. The rest of the steps would be the same.

@Override
public int compareTo(Object o) {
Employee employee = (Employee) o;
return this.getId() - employee.getId();

}

package sortingusingcomparable;

import java.lang.reflect.Field;
import java.util.*;

class Employee implements Comparable {
	String name;
	int id;

	public Employee(String string, int i) {
		this.name = string;
		this.id = i;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

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

	@Override
	public int compareTo(Object o) {
		Employee employee = (Employee) o;
		return this.getId() - employee.getId();

	}
}

public class SortingByIdUsingComprable {
	public static void main(String[] args) {
		List<Employee> emplist = new ArrayList<Employee>();
		emplist.add(new Employee("John", 101));
		emplist.add(new Employee("Eric", 106));
		emplist.add(new Employee("Arya", 103));
		emplist.add(new Employee("Sansa", 105));
		emplist.add(new Employee("Cersei", 102));

		Collections.sort(emplist);
		emplist.forEach(empolyee -> System.out.println(empolyee.getName() + "   " + empolyee.getId()));
	}
}

 

The output of the above program.

John 101
Cersei 102
Arya 103
Sansa 105
Eric 106

[stextbox id=’info’]How compareTo(Object o) method is getting called, we are not calling compareTo() explicitly.When we do Collections.sort(emplist) internally it calls compareTo(Object o) method.[/stextbox]

 

That’s all about Sorting using Comparable basis of id and name example in Java.

You may like.