This code does exactly what I want it to, but I would like to know how to code a compareTo method to use in my method that sorts and array of strings. Ive read up on the comparable class and compareTo and cant figure it out to save my life. thank you
What part don't you understand? The docs make it pretty clear: You look at the two objects under question, and if "this" is greater than the other object according to your rules, return a postive number. Negative if "this" is less, and zero if they're equal.
I understand how it works. But i can't simply say 01<02. That does not work. How do I compare to object without using <>or =??? and in the sort method would i do if(compareTo(temp1,temp2) = -1??? thank you
I can't imagine what I can tell you that's not there. If you don't show what you tried or tell us what part you didn't understand. Here's an example, but I'm sure similar examples would have been readily available in the search results.
Also, note that unless you're writing your own sort method, you'll never call compareTo directly. If you call Collections.sort(), it calls compareTo() on various pairs of values.
I find your question a little confusing, because the code you have shared already calls the compareTo method on the objects being sorted. But I have the feeling that you have missed something essential about the Comparable interface: classes which implement this interface work on themselves. That is to say: the compareTo method of a class is called on an instance of that class, and takes as a parameter another instance of that class (or, at any rate, an instance of a class that bears comparison to it). These two instances are the subjects of the comparison. So this class definition doesn't make much sense. It says to the world that instances of Sort can take an instance of type Object and determine which of the two should come first in a list. But you aren't sorting instances of Sort; there won't be any.