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 #31424 of 32213 |
Re: [Java] How to write do multifield sorting easily

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.
>

Look at the Comparable<E> interface that defines the "int compareTo(E)"
method. This accomplishes the same thing as the six comparison operators
(although you can argue that C++'s operator==() is more like Java's
equals() method). How it works is you compare the result of that to
zero. Here are some examples:

// C++
std::string str1("a");
std::string str2("b");
bool comparison = (str1 < str2);

// Java
String str1 = "a";
String str2 = "b";
boolean comparison = (str1.compareTo(str2) < 0);

Notice how the comparison operator is the same, and the strings appear
in the same order. Under the hood Java is performing an extra operation,
comparing the two strings and then comparing the result to zero. C++ is
performing one comparison, but hiding it behind a "primitive" operator
that really calls a function.

Throughout much of the Collections framework and elsewhere you will see
discussion about "ordering consistent with equals." It is possible that
object1.compareTo(object2) == 0, yet object1.equals(object2) is false.
BigDecimal is a perfect example: two decimals may be equivalent in value
yet not equal (2.0 and 2.00, for example). This is an extra layer of
stuff on top of what C++ has by default, and something you should keep
an eye on. It is neither better nor worse, just different, and something
that can cause subtle bugs if you are not used to it.

One last note: you are better off with a List than a Vector. C++ uses
std::vector as its basic dynamic array. Java uses it for the same
purpose, but it adds synchronization, which is slow. Prefer to use
ArrayList, which is not thread safe. If need be you can wrap it in a
synchronized facade (Collections.synchronizedList() I believe), or use
one of the concurrency framework lists that are copy-on-write and
implement the same List interface.

Another last note: iterators behave slightly differently in Java and
C++. While they solve similar problems, they do so differently. In
particular, there is no algorithms package that accepts iterators in
Java like there is in C++. You can do the same things, you just get
there a different way.

--
John Gaughan




Fri Jul 18, 2008 12:13 am

john23874
Offline Offline
Send Email Send Email

Forward
Message #31424 of 32213 |
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