compareTo() hands-on | sorting custom objects in java [LOGIC] | Comparable |#5

preview_player
Показать описание
In this video, we will learn about working with compareTo(). The compareTo() which is present inside the comparable interface returns 3 types of value eg. A +ve, -ve or a zero. The compareTo() used by the sort() to sort any kind of object.

As we have seen already, The Integer class and String class already has overridden the compareTo() and given there implementation like all other wrapper classes. Now we have to build a compareTo() for our song class to make it eligible for sorting. We can sort the song objects based on various property like an artist, song title track or by the release year.

So let’s dive in and explore the compareTo() method of comparable interface and sort some customize objects. Do remember that it is one of the most important interview question that you need to prepare.

For more videos in the collection framework, consider subscribing by clicking the link below.

Stay tuned and like my Facebook page for more.

Music :
-----------
credits : -
(intro)
Creative Commons — Attribution 3.0 Unported— CC BY 3.0

intro template :
Рекомендации по теме
Комментарии
Автор

i really like how u break down this topic in a step by step approach, starting with absolut basics and building knowledge on top of it.

vkqwhgx
Автор

Thanks for making this as simple as possible. It's good to get the logic down before trying to use compareTo in more complex situations.

InSterquiliniisInvenitur
Автор

Thanks a lot. I didn't find anywhere like this. Now we can easy to remember.

SaratKumar-qklu
Автор

Just amazing of the few best explanations on youtube... :)

TheProblemSolvers
Автор

Ooh god...no books can teach like sir !!!! need more videos on java

sivakumar
Автор

That was some classy video, thanks man :)

lordbaggot
Автор

Can anyone please tell me how internally does compare To works i.e., if i have list of 5 times then in first which two things will be compared

jakeg
Автор

Thank you, but willing to see how that happens implicitly, I just got confused how the sort method use our compareTo method, I know you mentioned that, but would you please show us that like in debugging mode or something like that! Thank You

Adam-gpij
Автор

Thanks.
In my case String getTitle() logic is not working. What might be the issue.? Can u plz tell me.

manojj
Автор

What is the need of compare () method if we are able to give our own logic in compareTo() method such as reversing the list?

somias
Автор

Hi Abhilash, Please make the Videos on JDK 1.8 New Features and Spring Boot also.

sha
Автор

please can explain how current object is compare with other object? how exactly it work?

ashishkeskar
Автор

Hi Abhilash its very awsome. I appreciate from heart. amazing
can you Explain ? What is natural and customized sorting order. I am very confused. what meaning of Natural sorting order(Comparable) and Customized Sorting
pls make this topic video
Thank you so much Abhi

AkashKumar-fwrg
Автор

Amazing video. great explanation.
Thank You for your video. It is really informative.
From where I can get the jar file for "MyUtils" package?

adeshpadwal
Автор

I'm not getting a compareTo method for getYear method
But it's coming for other getters like getArtist and getSongName
Anyone know?

santhoshchandran
Автор

How internally compairTo method call directly please tell me?

rahulpatil
Автор

In the debugger, for when the compareTo method we made in class Song(for comparing the which years are greater), i noticed that Song anotherSong will point to the 0 index of the arraylist object and the this.song points to the 1 index. I have no idea how this is even possible internally and why this is happening. Someone please help awnser this! Its driving me nuts

jonwinder
Автор

Hi abhilash thankyou for the explanation could.you please help me on how the negate int positive int and equal to works like does the system internally sort when negative int comes does it sort in ascending format ? and when positive int comes does it sort in descending format how could this logic work if ( x>y) return 1, if x<y return -1 please could you explain this in depth

saiprasad
Автор

Really great great video.
One doubt but how the CompareTo method of comparable is getting called.
Does when we say Collections.sort(songs), the compareTo method is getting called automatically??

abhishekavr
Автор

Lost on how the compareTo works internally when the iterates through it. I get that in the Song class, the (and since getTitle returns type string it will call the compareTo method in the String class since String class implements Comparable and has its own concrete definition of Compare to) compares the lexico of the words based on the first character of the word, then it compares the ascii values to see which word comes first based on subtracting the two ascii values like how a dictionary is or it compares the getYear and compares the difference between the years to see which year comes first based on positive and negatives i dont get how it iterates through the ArrayList object called musiclist one by one.

jonwinder
welcome to shbcf.ru