aspose file tools*
The moose likes Beginning Java and the fly likes How could i code a compareTo statement for this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How could i code a compareTo statement for this code" Watch "How could i code a compareTo statement for this code" New topic
Author

How could i code a compareTo statement for this code

Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
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

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.

Did you try googling for something like java compareTo example or java compareTo tutorial?
Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
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
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jim Stevens wrote: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 =???


Then you don't really understand how it works.

Do the searches I suggested.

and in the sort method would i do if(compareTo(temp1,temp2) = -1??? thank you


No, it would be:

The compareTo method is called on one of the two objects you're comparing. And you can't just assume compareTo will return 1 for x < y, only that it will return something < 0.
Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
I have done the searches. That is the reason I came to these forums. I am not looking for answer but maybe some guidance or explanation in english on how they work a little more....
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jim Stevens wrote:I have done the searches.


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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38769
    
  23
And have you looked through the String class to see whether it already has a compareTo method or implements Comparable (which is, by the way, not a class but an interface)?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How could i code a compareTo statement for this code