aspose file tools*
The moose likes Beginning Java and the fly likes [Comparable] problem with ording classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[Comparable] problem with ording classes" Watch "[Comparable] problem with ording classes" New topic
Author

[Comparable] problem with ording classes

Klaus Jesper
Greenhorn

Joined: Jan 20, 2005
Posts: 15
Hi, I have a problem to with sorting some elements.

I have a class Test1 which implements Comparable and so has as compareTo-method:



Next I have class Test2, that has a relation to Test1. Every Test2 has one associated Test1. To order a set of Test2-objects I want to use the associated Test1-objects (by comparing them throw compareTo()).



Now I want to order a Set of Test2 with the help of their associated Test1-class. But it doesn't work:



Any ideas what I'm doing wrong?

Klaus
[ January 30, 2005: Message edited by: Klaus Jesper ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Klaus Jesper:
Now I want to order a Set of Test2 with the help of their associated Test1-class. But it doesn't work
By this do you mean
  • You get a compile error
  • You get a runtime error (exception)
  • The program runs but the ordering is incorrect


  • ??? I like a good puzzle, but you have to give us all the pieces!

    This expressionlooks suspicious. Does Test2 contain the following member field?If not, Test2 doesn't really contain a Test1. If so, you need to reference Test2's field name -- not the name of the class the field references.
    Klaus Jesper
    Greenhorn

    Joined: Jan 20, 2005
    Posts: 15
    Sorry for my inaccuracy. "C: The program runs but the ordering is incorrect" is the problem the occurs.

    The correct code is:



    What I want to do is to sort a set of Test2-objects by using the compareTo-method of the associated Test1-object.
    [ January 31, 2005: Message edited by: Klaus Jesper ]
    Jeroen Wenting
    Ranch Hand

    Joined: Oct 12, 2000
    Posts: 5093
    That's not what Comparable is designed for.
    It's designed to determine the natural ordering for objects of the same type, not to provide a means in one class to order objects from another.


    42
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    But the natural order of Test2 objects is based on the values of their Test1 fields. The code snippet really looks just fine.

    Klaus, have you sorted a bunch of Test1 objects to verify their compareTo() is doing what you want? What type is orderNo? Is it an object that should be using compareTo() again instead of ">"?


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    Jeroen Wenting
    Ranch Hand

    Joined: Oct 12, 2000
    Posts: 5093
    In that case the Test2 class should implement Comparable and have that test on the Test1 field, not like this with Test1 implementing Comparable.
    David Harkness
    Ranch Hand

    Joined: Aug 07, 2003
    Posts: 1646
    Originally posted by Jeroen Wenting:
    In that case the Test2 class should implement Comparable and have that test on the Test1 field, not like this with Test1 implementing Comparable.
    I disagree. What if Test2s were being ordered by a String field in Test2? Would you reimplement String-comparison inside Test2 or use String's compareTo() method?

    Klaus, I suspect that in retyping those classes here you have managed to fix the problem. In other words, you probably have a typo in the originals. Why do I say this? Because I just ran your code (adding only constructors) and it works beautifully.The output is 1 ... 9, each on its own line, as expected.

    One thought. If you allow duplicate order numbers, you'll get incorrect behavior. You need to initialize returnValue to 0 in Test1 otherwise you return -1 for equal orderNo values. I assumed you didn't have duplicates.
    Jeroen Wenting
    Ranch Hand

    Joined: Oct 12, 2000
    Posts: 5093
    you call compareTo() on String from inside the compareTo() in your Test2 class.
    You can do the same thing here. But you're NOT supposed to expect to be able to compare 2 Test2 objects from inside a Test1 instance which is what the OP seems to be trying to achieve.

    So you'd get something like
    Klaus Jesper
    Greenhorn

    Joined: Jan 20, 2005
    Posts: 15
    Thanks for all answers!

    @Stan: My orderNo is of type int.

    @all: I'm sorry, but indeed it has been a stupid copy&paste-mistake by me.

    In the line that should be

    I actually didn't check for class Test2 but for another class. So I always got 1 as returnValue because the if-statement has been always true.

    Thanks again for your great suport!

    Klaus
    David Harkness
    Ranch Hand

    Joined: Aug 07, 2003
    Posts: 1646
    Originally posted by Jeroen Wenting:
    you call compareTo() on String from inside the compareTo() in your Test2 class.
    That's exactly what Klaus is doing; just replace "String" with "Test1". In fact, the code you posted is just a shorter version of Klaus's.

    Klaus, this is one reason I recommend pasting your broken code directly into your post when you can. I've seen this twice in the past week now, and it will make you go nuts.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: [Comparable] problem with ording classes