Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Collections.sort in 1.5

Thread view: 
Myriam Abramson - 30 Aug 2006 03:47 GMT
I have:

Vector<MyClass> v = new Vector<MyClass>();

Collections.sort(v);

Now, MyClass implements Comparable but Collections.sort seems to
ignore it. I would think that the order coded in the compareTo method
in MyClass would be the natural order of instances of this class, no?
I had to create a Comparator class to tell Collections.sort how to
sort correctly. Why having those annotations if they are not picked up
correctly? Again I have Java 1.5. Thanks for any insights.

                                  myriam
hiwa - 30 Aug 2006 04:05 GMT
Myriam Abramson のメッセージ:

> I have:
>
[quoted text clipped - 10 lines]
>
>                                    myriam
Your mplementation of equals() and hashCode() issue, or other
issue(s)...
Post a small demo code that is generally compilable, runnable and could
reproduce your problem. See:
http://homepage1.nifty.com/algafield/sscce.html
Patricia Shanahan - 30 Aug 2006 06:21 GMT
> I have:
>
[quoted text clipped - 8 lines]
> sort correctly. Why having those annotations if they are not picked up
> correctly? Again I have Java 1.5. Thanks for any insights.

Interesting. There has to be something special going on here, because
Collections.sort normally deals correctly with Comparable. I second the
request for a short, self-contained, compilable example.

I can think of some explanations, but they depend on how MyClass is
defined. For example, it could extend a Comparable class and not itself
provide a public compareTo(Object), only a compareTo(MyClass). In that
case, the sort would be according to the parent class natural order.

The quickest way to find out what is really going on will be an example.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.