How to sort a list by name using a Comparator interface in Java?

Java 8Object Oriented ProgrammingProgramming

Comparator interface can be used to order the objects of user-defined classes. It is capable of comparing two objects of two different classes. We can sort a list of objects where we can't modify the object’s source code to implement Comparable interface. A lambda expression can't be executed on its own and used to implement methods that are declared in a functional interface.

In the below example, we need to sort a list by name using the Comparator interface and Stream API with the help of lambda expressions.

Example

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class ListSortByNameTest {
   public static void main(String[] args) {
      List<Person> arrayList = new ArrayList<Person>();

      Person p = new Person();
      p.setName("Adithya");
      p.setAge(20);
      arrayList.add(p);

      Person p1 = new Person();
      p1.setName("Jai");
      p1.setAge(25);
      arrayList.add(p1);
     
      Person p2 = new Person();
      p2.setName("Surya");
      p2.setAge(33);
      arrayList.add(p2);

      Person p3 = new Person();
      p3.setName("Ravi");
      p3.setAge(35);
      arrayList.add(p3);

      Stream<Person> stream = arrayList.stream();
      stream.forEach(x -> System.out.println("Name: [" + x.getName()+"] Age: ["+x.getAge()+"]"));

      Comparator<Stream> byPersonName = (Person p4, Person p5) -> p4.getName().compareTo(p5.getName());
      Stream<Person> sorted = arrayList.stream().sorted(byPersonName);
      sorted.forEach(e -> System.out.println("Sorted Name: [" + e.getName()+"] Age: ["+e.getAge()+"]"));
   }
}

// Person class
class Person {
   public String name;
   public int age;
   public String getName() {
      return name;
   }
   public int getAge() {
      return age;
   }
   public void setName(String name) {
      this.name = name;
   }
   public void setAge(int age) {
      this.age = age;
   }
}

Output

Name: [Adithya] Age: [20]
Name: [Jai] Age: [25]
Name: [Surya] Age: [33]
Name: [Ravi] Age: [35]
sorted Name: [Adithya] Age: [20]
sorted Name: [Jai] Age: [25]
sorted Name: [Ravi] Age: [35]
sorted Name: [Surya] Age: [33]
raja
Published on 02-Jan-2020 06:44:38
Advertisements