 Hello and welcome to the lecture on Comparable Interface in Java. In this lecture we will be discussing need of comparable interface and use of comparable interface to sort group of objects. The learning outcome of this video lecture is that the student will be able to explain need of comparable interface and write a Java program using comparable interface in Java to sort group of objects. Learning is an operation performed on list of elements in which elements are arranged in order sequence that is ascending or descending order. If you want to sort numbers it will be in ascending or descending order. Here is a list of numbers sorted in ascending order. But if you want to sort objects such as apples, if you want to sort then how should we compare and sort apples? Do we want to sort them by weight? If so are we sorting them from lightest weight to heaviest or heaviest to lightest? So that is one of the attributes weight we are considering here. When we are sorting them we need to repeatedly compare to apples weights until all the apples are in the correct order. Is apple 1 heavier than apple 2? Is it heavier than apple 3? We need to keep doing that until the list is sorted. So here weight attribute is used to compare to apples. The comparable interface helps us accomplish this goal. Comparable can't sort the objects on its own but the interface defines a method compared to. Class need to implement comparable interface to override this interface. So here we will see one example sorting of operation on a group of objects. So here we are considering those group of objects of student class. So we can see here S1, S2, S3, S4, S5, S6 these are the student class objects and the data members are roll number, name and score. Now here if you want to sort this group of student objects on roll number based on the roll number field in ascending order then it will be arranged like this. First S1 roll number 10 then compared then roll number 13 then roll number 15 will be arranged then 18 then roll number 19 and roll number 22. Second way we can arrange this list of student objects or group of student objects based on name field or based on score field. So that is the main use of the comparable interface to sort the group of objects. These objects can be a user defined class objects or building class objects. A comparable object is capable of comparing itself with another object. The class itself must implement the comparable interface to compare its instances. The class implementing comparable interface has to overwrite compare to method. In this method we have to write the logic for sorting. The compare to method signature is shown here. The class is returning integer value and object type parameter is passed to this method. The current object is compared with the passed object to arrange as per order. The compare to method call is shown here that is object 1, obj1 is calling compare to method and object 2 that is obj2 is passed as a parameter to compare to method. The compare to method is used to compare these two objects. The compare to method returns minus 1 if object 1 is smaller than object 2. The compare to method returns 0 if the object 1 is same as object 2. The compare to method returns 1 value if object 1 is greater than object 2. If any class implements comparable interface in java then collection of that object either list or array can be sorted automatically by using collections.sort or arrays.sort method. And objects will be sorted based on their natural order defined by compare to method. Here is the sample code using comparable interface. In this code country class implements a comparable interface therefore compare to method has been overridden here. The logic of comparison of two country objects written here accordingly value written by the compare to method. If the current objects country id is smaller than the past objects country id then minus 1 is written. If the current objects country id is greater than past objects country id then 1 is written. If the current object country id is equal to past objects country id then 0 value is written. Now you can pause the video for some time and think and answer the following question. If the class implements comparable interface then which method needs to be overridden by the same class. Option A compare method which is returning int value and 1 object is passed. Option B compare to method returning int value with 1 parameter of type object. Option C compare to method returning void without parameter and option D none of the above. Here the correct answer is compare to method which is need to be overridden by the class which is implementing comparable interface. Here is the sample program in which person objects will be sorted based on the person name field. Therefore, person class implements comparable interface. Class person consists name and country as data members. Next is the constructor of person class through which name and city field values initialized. Then getter methods of name and country field. Then compare to method implemented in which name field of the current object and name field of past object compared and accordingly value written by the compare to method. The compare to method implemented in which the name field of the current object and name field of past object compared and accordingly value written by compare to method of string class. The same value is written from comparative method of person class. Now in the demo comparable class, first we will be creating a list of type person to store person objects. Then in this array list add person object as shown here in which first person object is with name Robert and country USA. Second person object with name Andy and country UK and third person object with name Harish and country India. Now array can be sorted automatically by using collections.sort method called in which ArrayList object is passed to sort method. Now to display name of person from ArrayList here we have used for loop to access each person object from ArrayList. This sort method called sort the ArrayList based on the name field. Therefore output of the program is as shown here that the list of the person is sorted based on the name field. Let us see one more program here. Here class author implemented the comparable interface. Now in the author class we have defined two data members first name and last name. Then constructor of the class through which we are initializing the values of first name and last name. Then getter method to get the first name and last name data member values. Here the comparative method has been overwritten by the class author in which the last name field here the last name field has been compared by invoking the comparative method of the string class and the value written by the comparative method of string class that value is written by this comparative method which is a part of the author class. Then in the demo comparable class we are creating this ArrayList of author objects in which we have added the three author objects. First one is with the values Yeshwant, Kanatkar, second one is Peter, Galvin and third one with the Herbert shield. So in this ArrayList there are three author objects added. Then collections.sort called, sort method from the collection is called. Here the parameter past is the ArrayList of author objects. Then we are iterating this list of author objects and displaying the values from the list of author objects that is the first last name has been displayed and then first name is displayed. So in this author class we have overwritten this comparative method to compare the last name field. So means sorting will be done based on the last name field. So the output will be the sorting based on the last name field that is the Galvin, then Kanatkar and then Shield. So this is one more example to understand how the comparable interface can be used within the class to sort that object of that particular class. Now here partial code has been given so we have to complete this code. So in this Gilesas quickly explained, we are creating the employee class which is implementing the comparable interface and in that two data members are their employee ID and name. Name of the employee through the constructor ID and name has been initialized. Then Gator method is there for employee ID and for name. So in this we are left the comparative method implement definition for you. So in the demo class we are creating the array list of employee objects in which three objects have been added here and the collection using the collection.sort method we are sorting this array list of employee objects and next using the loop for loop we are iterating that list and displaying the employee ID and name. So as per the expected output here, expected output is that this list will be sorted based on the employee ID in ascending order. So what line of code need to be written to sort the list of group of employee objects based on employee ID field. So think over it for a while and come with your answer in the lines of code need to be a part of the comparative method. So here we want to sort this list of employee object based on the employee ID field which is of type integer. So we are going to compare that employee ID if the current object employee ID is smaller than the employee ID of the object which is passed as a parameter to compare to method then minus one is written. If the current object employee ID is greater than the parameter employee ID then one is written else means both is same then zero value is written by this but it compare to method. So then you will get the list of objects arranged in ascending order based on the employee ID field. Thank you for watching this video.