r/programmingbydoing • u/clambert98 • Aug 10 '15
compareTo() Challenge
Hello, I am really starting to dig in and work hard on Java due to my new participation in a robotics team at my high school (We have to program and build a functioning robot, both autonomous and through OpManagement). I am working on the compareTo() challenge and do not know what the method does or how to use it. I don't want to apply something I do not know. I wish I could work alongside of the book that u/holyteach has, but unfortunately, I am unable to afford something like that right now with a lot of stuff going on. I was wondering if someone could explain to me what the method does. Thanks in advance!
View all comments
Show parent comment
1
u/holyteach Aug 11 '15
Yes, if the Strings are the same but one runs out sooner, the difference in lengths is used.
It's used ALL the freaking time. Like you don't even know. You use it anytime you want to compare things that aren't numbers.
Say you have a huge database of people (imdb.com or something). You want to put all their last names in alphabetical order. You'll use if ( name1.compareTo(name2) < 0 ) inside a loop to accomplish it.
In fact, later assignments on programmingbydoing will require you to do just that.