 Hello, and welcome to the lecture on Comparator Interface in Java. In this lecture, we will be discussing about need of comparator interface and use of comparator interface to sort group of objects. The learning outcome of this video lecture is that the student will be able to explain need of comparator interface and able to write a Java program using comparator interface in Java to sort group of objects. Computing is an operation performed on the list of elements in which elements are arranged in order sequence, that is in ascending order or in descending order. Comparator interface is used to order the objects of user defined classes. Comparator object is capable of comparing two objects of two different classes. Suppose, we have an array of our own class type student containing fields like roll number, name, address, date of birth and we need to sort the array based on roll number or name of student. Here approach one can be write your own sort function using one of the standard algorithms. This solution requires rewriting the whole sorting code for different criteria like roll number and name. Approach two can be using comparator interface. Comparator interface is used to order the objects of user defined class. This interface contains two methods that is first one is a compare method with two parameters and equals method. Here we will be discussing about the compare method. Class whose object to be sorted do not need to implement this interface. Separate class need to be defined which implement this interface. Using comparator we can sort the elements based on data members. For instance it may be on roll number, name or age or any other data member. Now here we will see one example of sorting student class object based on roll number field. Here the six student objects instance is shown. Every instance is having the field roll number, name and score. If you want to sort this object based on the roll number how it will be? So in the ascending order, so roll number 10 then roll number 13, roll number 15 then roll number 18, roll number 19 and then 22. Now this way we have sorted the list of student objects based on roll number field. Now if you want to sort this same set of student class objects based on name field, other field other than roll number. So how it will be? Then first one will be the name Ajay, alphabetically if we go, then Arti that is second, then Deepa that is S4 student object, then Pia, then Sanu and then the Yogi. So now this time the student objects are sorted based on the name field. So we can sort the same set of objects based on different attributes from the same class. To enable total ordering on objects we need to create class which implements the comparator interface. Then we need to override its compare method, compare method with two parameters, object one and object two. The comparator interface is used when an extra logic is required to sort the objects. In the compare method the logic of sorting will be written. We can implement different sorting based on different attributes of objects to be sorted. This interface is present in java.util package and the class which implements this interface need to override the compare method. The compare method signature is shown here. The class, the particular method which is returning integer value. The compare method signature is shown here. The class returning integer value and object type parameter is passed to this method. The compare method signature is shown here. The parameter passed to this method is two objects. It compares these two objects which is passed as an argument to arrange as per order. The compare method is used to compare these two objects. Now the return type of this method is an integer value. The compare method returns minus one if object one is smaller than object two. It returns zero if object one is same as object two and this method returns positive integer if object one is greater than object two. Here is the example code using comparator interface. In this code country sort by id class implements the comparator interface to sort collection of country objects by country id. Therefore compare method has been overwritten here. The logic of comparison of two country objects return here and accordingly value written by a compare method. If country one first objects country id is smaller than country two second objects country id then minus one is written. If country one first objects country id is greater than country two second objects country id then one is written and if country one first objects country id is equal to country two second objects country id then zero value is written from the compare method. Now you can pause the video for some time think and answer the following question. If the class implements comparator interface then which method needs to be overwritten by the same class. Option A compare two method with two parameters option B compare method with one parameter option C compare method with two parameters option D none of the above. Here the correct answer is C that is int compare two parameters object one and object two method needs to be overwritten by the class which is implementing comparator interface. Here is the sample program in which student objects will be sorted based on the student age and name field. In the student class three data members define our role number name and age then student constructor define with three parameters role number name and age. Here on right side age comparator class is defined which implements comparator interface. So compare method is overwritten by the age compare age comparator class. In the compare method the logic of age field comparison is written here. First pass two objects typecasted into student type objects and define local references S1 and S2 then age field is compared and accordingly value is written. If age is equal to zero value if age of these two student objects are equal then zero value is written. If S1 is value is greater than S2 then one is written. If S1 is smaller than S2 then minus one is written. Now here name comparator class is defined which implements comparator interface. So compare method is overwritten by the name comparator class here. In the compare method the logic of name field comparison is written here. First pass two objects typecasted into student type objects and define local references S1 and S2 then S1's name field is compared with S2's name field. Here compare to method of string class is called to compare two string objects therefore the value written by the string class compare to method is the same value written by compare method of name comparator class. Now in the demo comparator class first we will be creating array list of type student to store student objects. And in the array list add student objects as shown here in which first student object is with roll number 101 name Vijay and age 23. Second student object with roll number 106 name Ajay and age 27. And third student object with roll number 105 name J and age 21. Now array can be sorted by using collections.sort method call in which array list object and the comparator is passed to sort the method. In the first sort method call array list and name comparator is passed as arguments. Now to display the information of student from array list here we have used iterator and while loop to access each student objects from array list. The sort method call sort the array list based on the name field therefore the output of the program is as shown here that the list of student objects is sorted as per the name of the students. Now in the second sort method call array list and age comparator is passed as the arguments to the sort method. Here now to display the information of students from the array list that is after sorting here we have used iterator and while loop again to access the each student objects from the array list. The sort method call sort the array list based on the age field therefore the output of the program is as shown here. Now the array list of student object is sorted as per the age field of the student object in ascending order. So this is an example of using the comparator interface on two different fields that is a roll number sorry that is a name and age of the student. So this is an example of using name comparator interface to sort the student object based on the name field and age field of the student object. Thank you for watching this video.