This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sorting Collections Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sorting Collections Doubt " Watch "Sorting Collections Doubt " New topic
Author

Sorting Collections Doubt

aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Hi,

If, there are two or more elements are same in a list(dvdList). I get different output at different time of execution. Is that how sorting algorithm work in collections?




Thanks
Saritha
[ September 27, 2008: Message edited by: saritha babu ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Could you show us the comparator code? Could you show us sample data? Could you show us sample output? Etc...

Obviously, a sorting algorithm that sorts differently, depending on time of day, is *not* how it is supposed to work.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Could you show us the comparator code? Could you show us sample data? Could you show us sample output? Etc...


Hi Henry,

Here is the Comparator code...



first output is:

[Raiders of the Lost Ark action Ford, Harrison
, Patriot Games action Ford, Harrison
, Caddy Shack comedy Murray, Bill
, Lost in Translation comedy Murray, Bill
, Donnie Darko sci-fi Gyllenhall,Jake
, 2001 sci-fi ??
, Star Wars sci-fi Ford, Harrison
]
second output is:

[Patriot Games action Ford, Harrison
,Raiders of the Lost Ark action Ford, Harrison
, Caddy Shack comedy Murray, Bill
, Lost in Translation comedy Murray, Bill
, Donnie Darko sci-fi Gyllenhall,Jake
, 2001 sci-fi ??
, Star Wars sci-fi Ford, Harrison
]

Thank you
Saritha
Shiraz Choudhary
Greenhorn

Joined: Aug 20, 2008
Posts: 4
Hi Saritha!
I am not sure if i am right, but i am guessing that you are only sorting the collection just by the genre.
The "toString()" method for "DVDInfo" object is this:

then the output you have is sorted by genre.
Look at it again, genre is sorted.

........action..............
........action..............
........comedy..........
........comedy.........
........sci-fi........
........sci-fi........
........sci-fi.......



(in both outputs)

The collection is not sorted by the title so the DVDInfo objects that has the same genre but different titles can switch their order but only within each other.
You will not have comedy or Sci-fi come before action, no matter how many times you run the code.

It would be easy to answer your question if you write all of the code for this program. Like how you are sorting (compareTo or Compare method) the main method etc.

[ September 27, 2008: Message edited by: Shiraz Choudhary ]
[ September 27, 2008: Message edited by: Shiraz Choudhary ]

SCJP 6.0
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Hi Shiraz Choudhary,

It would be easy to answer your question if you write all of the code for this program. Like how you are sorting (compareTo or Compare method) the main method etc


Here is the code...
I think I am little confused. I get the same result when I execute this time. The result in book(pg 555) and mine is different. Thanks for the help.



Thanks
Saritha
 
Don't get me started about those stupid light bulbs.
 
subject: Sorting Collections Doubt
 
Similar Threads
Need help in understanding how Comparable and Comparator works ?
collection problem?? please solve
Comparable vs Comparator
Sorting objects using comparator doesnt work
Reading data from a file and storing into array of strings problem