• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How could i code a compareTo statement for this code

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)?
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic