00:00Next, let's talk about how we can sort the elements in the area list.
00:04Now, what we have learned about the sorts,
00:06such as we have bubble sort, selection sort, insertion sort,
00:13besides the recursion, I have studied merge sort, quick sort,
00:18I also studied count sort.
00:21So, we can put all the sorting directly on the area list.
00:25And we can implement all the sorting from scratch.
00:29But here, I'm going to talk about the inbuilt method.
00:34I will discuss one of the optimized methods,
00:37which I don't need to implement in scratch,
00:39which already exists in Java.
00:41If I have to sort an array list in Java,
00:44then I simply write collections.sort,
00:48which I pass in my list.
00:51Like, if I had to sort an array in the array,
00:55then what would I write?
00:56Inbuilt method, I would write arrays.sort
00:59and pass my array, which is called ARR.
01:05So, if I have written this statement in Java,
01:08then my ARR will sort.
01:10What would happen in this array?
01:12It was my array class.
01:14This class was my sort method.
01:17In which the array object is passed,
01:19it will be sorted.
01:22We can do the same thing in the array list.
01:24The class we use here will be the collections class.
01:27In Java, there is a collections class,
01:30which we are going to use for the array list.
01:34In Java, there is a collection.
01:36The collection is not plural,
01:39it is singular.
01:39It is an interface.
01:41And this is my class.
01:43When I sort it,
01:45I have to use collections class.
01:47I don't have to use collection interface.
01:49Here, there is a last one.
01:51So, this is important.
01:53Many students only write collections.
01:55In that case, they will have an error,
01:57that this method doesn't exist,
01:58or it will be related to the interface.
02:00So, if you have an error,
02:02then I have to understand that
02:03we are going to use the interface,
02:04but not the class.
02:05So, we have to use this class
02:06whenever we sort the array list.
02:09I will call the sort method in the collections class.
02:13If I pass the list in which I have my list,
02:15then my list will be sorted in ascending order.
02:18This is important to note.
02:20Here, the array is sorted in ascending order.
02:22And from collections.sort,
02:24the list is sorted in ascending order.
02:27Now, let's write code.
02:29I have made a list in the main function,
02:31which I have added some random elements.
02:33which I have added.
02:34Now, what will I do?
02:35I will write collections.sort.
02:38I will pass the list.
02:40Now, as we write collections here,
02:42there will be a grad line that
02:43the collections cannot be resolved.
02:44This means,
02:45I don't know about the classroom class of this class.
02:49So, the collections,
02:51as I have imported the array list on the top of the array list,
02:53you have to import it.
02:55Import java.util.collections.
03:00I have imported the collections class.
03:02I have removed the error.
03:02This list is sorted.
03:03This list is sorted.
03:04I will print the initial list first.
03:08After sorting it,
03:09I will print my final list.
03:13Let's save and now run.
03:16So, the first is 25936,
03:19which has random numbers.
03:20After that, 23569.
03:22This list is a sorted list in ascending order.
03:26Now, with this list, I can import it in a single line.
03:30I can also import it in a single line.
03:31I can write import java.util.all.
03:35Star means all.
03:36So, these two classes,
03:37other classes,
03:39other classes,
03:39other methods,
03:40other interfaces,
03:41they have imported it together.
03:42The code will run again.
03:45Now, I don't have to import it in a single line.
03:50I can import it in a single line.
04:06I will pass the list as my first parameter.
04:09But, I am going to pass a second parameter.
04:11In the second parameter,
04:12I will pass the collections.reverseOrder.
04:16And this line will sort my array list in descending order.
04:21Now, how will it sort in descending order?
04:23Here, the second parameter is collections.reverseOrder.
04:26This is a comparator.
04:29Comparators are functions in java,
04:31which define the logic of sorting.
04:36If we have a list,
04:38it should be sorting by ascending order.
04:41But, the logic of descending order
04:43is written in this comparator function.
04:45So, I can imagine comparators as a function in java,
04:49which defines the logic of sorting.
04:53And these comparators,
04:54we don't apply the list on the list.
04:56We can apply the objects on the list.
04:59It is possible that I have written many names
05:01and I have made a list or a class.
05:04And there are many objects.
05:06So, what is the logic of sorting the objects?
05:09These are the things that you can't see by sorting.
05:13You can also make the logic of sorting.
05:14You can also make the logic of sorting.
05:15So, if you want to sort from A, B, C, D and A, B, C, D and A, B, C,
05:21D and A, B, C, D.
05:42Now, I have to remember this.
05:44What is a comparator in Java?
05:46A comparator is a function that defines the logic of sorting.
05:50So, what did I do?
05:51The logic of sorting by default is ascending order.
05:54But, I have passed the comparator here.
05:57So, the order of sorting, the logic of sorting,
06:00the logic of sorting,
06:01After calling this descending order,
06:04now, I will print my list again.
06:10So, the list is ascending order here.
06:14After reverse sorting,
06:15my list is in descending order.
06:18And this dot sort function is optimized.
06:22I mean, if I sit in a coding contest or in an interview,
06:26so, when I don't answer the logic of sorting directly,
06:29I can apply this dot sort function directly,
06:31and apply it to your list or dot sort function,
06:34and apply it to your array.