Sorting using Comparator in java with example


Table of Contents

sorting using comparator by name ——

3 gopi
2 krishna
5 mohan
1 radhika

sorting using comparator by id ——–

1 radhika
2 krishna
3 gopi
5 mohan

Example of sorting using Comparator in java implementing Comparator interface.

we can define a separate class which will implement Comparator interface.

import java.util.*;
import java.io.*;
class Employee {
 String name;
 int id;
 Employee(String name, int id) {
  this.name = name;
  this.id = id;
 }
}
class NameComparator implements Comparator {
 @Override public int compare(Object o1, Object o2) {
  Employee e1 = (Employee) o1;
  Employee e2 = (Employee) o2;
  return e1.name.compareTo(e2.name);
 }
}
class IdComparator implements Comparator {
 @Override public int compare(Object o1, Object o2) {
  Employee e1 = (Employee) o1;
  Employee e2 = (Employee) o2;
  return e2.id - e1.id;
 }
}
public class SortByUsingComparator {
 public static void main(String[] args) {
  List emplist = new ArrayList();
  emplist.add(new Employee("rakesh", 105));
  emplist.add(new Employee("bittu", 103));
  emplist.add(new Employee("tituu", 110));
  emplist.add(new Employee("asharaf", 108));
  System.out.println("sorting by name-------");
  Collections.sort(emplist, new NameComparator());
  Iterator itr = emplist.iterator();
  while (itr.hasNext()) {
   Employee emp = (Employee) itr.next();
   System.out.println(emp.name + " " + emp.id);
  }
  System.out.println("sorting by id---------");
  Collections.sort(emplist, new IdComparator());
  Iterator itr2 = emplist.iterator();
  while (itr2.hasNext()) {
   Employee emp = (Employee) itr2.next();
   System.out.println(emp.name + " " + emp.id);
  }
 }
}

The output of the above program is –

anjali 108
bittu 103
rakesh 105
shibha 110

sorting by id———

shibha 110
anjali 108
rakesh 105
bittu 103

Example of Sorting using Comparator and Java 8.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Employee {
 private String name;
 private int id;
 Employee(String name, int id) {
  this.name = name;
  this.id = id;
 }
 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;
 }
}
public class SortingUsingJava8 {
 public static void main(String[] args) {
  List employeeList = new ArrayList();
  employeeList.add(new Employee("rakesh", 105));
  employeeList.add(new Employee("bittu", 103));
  employeeList.add(new Employee("shibha", 110));
  employeeList.add(new Employee("anjali", 108));
  System.out.println("sorting on basis of name-------");
  Collections.sort(employeeList, (employee1, employee2) -> employee1.getName().compareTo(employee2.getName()));// Lambda expression for sorting on basis of name
  for (Employee employee: employeeList) {
   System.out.println(employee.getName() + " " + employee.getId());
  }
  Lambda expression
  for sorting on basis of id System.out.println("sorting on basis of id-------");
  Collections.sort(employeeList, (employee1, employee2) -> employee1.getId() - employee2.getId());// Lambda expression for sorting on basis of id
  for (Employee employee: employeeList) {
   System.out.println(employee.getName() + " " + employee.getId());
  }
 }
}
The output of above program.

Sorting on basis of name——-

anjali 108
bittu 103
rakesh 105
shibha 110

Sorting on basis of id——-

bittu 103
rakesh 105
anjali 108
shibha 110

Soring list of an object using Comparator.comparing().

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class Employee {
 private String name;
 private int id;
 Employee(String name, int id) {
  this.name = name;
  this.id = id;
 }
 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;
 }
}
public class SortingUsingJava8 {
 public static void main(String[] args) {
  List employeeList = new ArrayList();
  employeeList.add(new Employee("rakesh", 105));
  employeeList.add(new Employee("bittu", 103));
  employeeList.add(new Employee("shibha", 110));
  employeeList.add(new Employee("anjali", 108));
  System.out.println("sorting on basis of name-------");
  employeeList.sort(Comparator.comparing(Employee::getName));
  for (Employee employee: employeeList) {
   System.out.println(employee.getName() + " " + employee.getId());
  }
  System.out.println("sorting on basis of id-------");
  employeeList.sort(Comparator.comparing(Employee::getId));
  for (Employee employee: employeeList) {
   System.out.println(employee.getName() + " " + employee.getId());
  }
 }
}
The output of the above program. Sorting on basis of name: anjali 108 bittu 103 rakesh 105 shibha 110 Sorting on basis of id: bittu 103 rakesh 105 anjali 108 shibha 110

Learn more about collection in java


%d bloggers like this: