• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another question about using comparator

 
FY Hsieh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I have



Question --- when I sort a list of object "Student", since "Student' object contains "Address", and both of them implement Comparator, when "student" object list is sorted, does it automatically sort the associated "Address" objects using the "Address" compare rule ? or does it just sort 'student" obj list using its own rule without looking at "address"'s 'compare' rule ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when "student" object list is sorted, does it automatically sort the associated "Address" objects using the "Address" compare rule ?

No. You have to call the Address#compare yourself.
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Firstly shouldn't you be implementing the java.lang.Comparable interface rather than java.util.Comparator?

Secondly as Christophe said you it won't sort them automatically, you'd need to make some kind of call to the Address compare/compareTo method.

Sean
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Clark wrote:Firstly shouldn't you be implementing the java.lang.Comparable interface rather than java.util.Comparator?

I agree. I already mentioned it here.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a related subject, I see "return -1;" after your conditional statement.
Since Java does not support using numerical values for T/F conditions,
a boolean (or Boolean) should always be used for this purpose.

Jim ... ...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Hoglund wrote:On a related subject, I see "return -1;" after your conditional statement.
Since Java does not support using numerical values for T/F conditions,
a boolean (or Boolean) should always be used for this purpose.


Well, no. The compareTo()/compare() methods in Comparable and Comparator, respectively, return an int, so that's what he's returning. Each methods compares two objects and returns a negative number if the first one is "first", 0 if they're "the same" and a positive number if the second one should come first. His pseudocode leaves the "int" return type declaration off of his method -- this is an argument for why you should always PostRealCode .
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry - my bad. For some reason, I had equals() stuck in my head.
And yes, if I had seen the return type, it may have cleared my head.

Jim ... ...
 
FY Hsieh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why I should use comparable instead of comparator ? I read (can't remember where) comprable is more like for natural order and comparator is for customized order, is that true ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a class X, and you want to compare instances of X, you can do one of two things:

1) have X implement Comparable<X>, and then you can call "x1.compareTo(x2)"

2) create some other class entirely Z, and have it implement Comparator<X>; then you can call z.compare(x1, x2)

What you've done is create a situation where you can call x3.compare(x1, x2), and while it does work, it really doesn't make much sense. That object "x3" shouldn't be involved in the comparison. If you want a third object to compare two Xs, then make it an instance of another class. If you want one X to compare itself to another, then use Comparable.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll resist the temptation to confuse things even more by showing people how to write their Comparators as anonymous classes
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell : Please don't resist . . .

Jim ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic