Comparator Interface


                 Comparator is used to multiple sequence sorting. It is available in java.util package. In comparator two methods available- compare() and equals(). 

Comparator interface compare(Object o1, Object o2) method need to be implemented that takes two Object argument, it should be implemented in such a way that it returns negative int if the first argument is less than the second one and returns zero if they are equal and positive int if the first argument is greater than the second one.

                          package com.csi.collectionconcept; 

                          import java.util.ArrayList; 

                          import java.util.Collections; 

                          import java.util.Comparator; 

                          import java.util.List; 

                          class Customer { 

                                    int customerId; 

                                    String customerName; 

                                    int customerAge; 

                                    public Customer(int customerId, String customerName, int customerAge) { 

                                           super(); 

                                           this.customerId = customerId; 

                                           this.customerName = customerName; 

                                           this.customerAge = customerAge; 

                                   } 

                                   public String toString() { 

                                                return "Customer [customerId=" + customerId + ", customerName=" + customerName + ", customerAge=" + customerAge + "]"; 

                                  } 

                          } 

                         class SortByName implements Comparator { 

                         public int compare(Customer c1, Customer c2) { 

                                         return c1.customerName.compareTo(c2.customerName); 

                           } 

                 } 

                class SortByAge implements Comparator { 

                            public int compare(Customer c1, Customer c2) { 

                            if (c1.customerAge == c2.customerAge) { 

                                        return 0; 

                  } 

                              else if (c1.customerAge > c2.customerAge) { 

                                         return 1; 

                 } 

                               else { 

                                          return -1; 

                      } 

              } 

       } 

       public class ComparatorConcept { 

                               public static void main(String[] args) { 

                               List customerList = new ArrayList<>(); 

                               customerList.add(new Customer(126, "TOM", 23)); 

                               customerList.add(new Customer(129, "JERRY", 25)); 

                               customerList.add(new Customer(145, "RABEA", 20)); 

                               System.out.println("Sort By Name");

                               Collections.sort(customerList, new SortByName()); 

                               customerList.forEach(custname -> System.out.println(custname)); 

                               System.out.println("Sort By Age"); 

                               Collections.sort(customerList, new SortByAge()); 

                               customerList.forEach(custage -> System.out.println(custage)); 

                     } 

            }

 

                   Output:

                                  Sort By Name 

                                  Customer [customerId=129, customerName=JERRY, customerAge=25] 

                                  Customer [customerId=145, customerName=RABEA, customerAge=20] 

                                  Customer [customerId=126, customerName=TOM, customerAge=23] 

                                  Sort By Age Customer 

                                  [customerId=145, customerName=RABEA, customerAge=20] 

                                  Customer [customerId=126, customerName=TOM, customerAge=23] 

                                  Customer [customerId=129, customerName=JERRY, customerAge=25]