Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting Collections Doubt

 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20989
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic