Search the web
Sign In
New User? Sign Up
Java_Official · Java SE . EE . ME . AJAX . Web services
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Show off your group to the world. Share a photo of your group with us.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
How to write do multifield sorting easily   Message List  
Reply | Forward Message #31426 of 32218 |
Re: [Java] How to write do multifield sorting easily

This is the complaint that I had when I moved from C++ to Java. How
could a language so advanced not have overloaded operators?

Well, that's a choice (a poor one, IMHO) that the designers of Java
made, and we poor schmucks have to live with it.

There's a couple of ways you can get around this:
1) Use the hashCode () method on both objects. Every Java object has this.
TypeA objA = new TypeA (...);
TypeA objB = new TypeA (...);
if (objA.hashCode () < objB.hashCode ()) {
...

2) Use the equals () method. Every Java object has this.
TypeA objA = new TypeA (...);
TypeA objB = new TypeA (...);
if (objA.equals (objB)) {
...
Of course, this may mean that TypeA must override the default behavior
of comparing the references for equality.

3) Implement the compareTo () method. This is available by implementing
the Comparable interface. This method is used extensively by objects
that implement Comparator. TreeSet and TreeMap use Comparator objects
under the hood to sort their objects. So:
TypeA objA = new TypeA (...);
TypeA objB = new TypeA (...);
TreeSet<TypeA> ts = new TreeSet<TypeA> ();
ts.add (objA);
ts.add (objB);

will put objA and objB in order, according to the compareTo () method.

Hope this helped.

-Java Guy


samir wrote:
>
> Recently, I moved to Java from C++ as I love the language and its features
> very much. However, there is a little problem I am facing while writing
> comparison functions. In C++, it was easy to overload the < operator for
> user defined class. But how to achieve the same elegantly in Java while
> using custom classes? I will need to use these because of sorting
> vectors in
> out-of-the-norm fashion.
> Thank you in advance...
>
> [Non-text portions of this message have been removed]
>
>



Fri Jul 18, 2008 1:53 am

mmjavaguy
Offline Offline
Send Email Send Email

Forward
Message #31426 of 32218 |
Expand Messages Author Sort by Date

Recently, I moved to Java from C++ as I love the language and its features very much. However, there is a little problem I am facing while writing comparison...
samir
samir0505005
Offline Send Email
Jul 17, 2008
7:57 pm

Quick and shallow answer: If I'm not mistaken, you have to extend (or implement, not sure) Comparable and use Collection.sort() method. Regards, Rodrigo...
Rodrigo Coin Curvo
rodrigoccurvo
Offline Send Email
Jul 18, 2008
7:52 pm

... Look at the Comparable<E> interface that defines the "int compareTo(E)" method. This accomplishes the same thing as the six comparison operators (although...
John Gaughan
john23874
Offline Send Email
Jul 18, 2008
7:52 pm

This is the complaint that I had when I moved from C++ to Java. How could a language so advanced not have overloaded operators? Well, that's a choice (a poor...
Java Guy
mmjavaguy
Offline Send Email
Jul 18, 2008
7:52 pm

Thank you very much for your replies.they were indeed helpful! ... -- Sent from Gmail for mobile | mobile.google.com samir http://techblog-samir.blogspot.com...
samir
samir0505005
Offline Send Email
Jul 22, 2008
4:16 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help