File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes another question about using comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "another question about using comparator" Watch "another question about using comparator" New topic
Author

another question about using comparator

FY Hsieh
Ranch Hand

Joined: Aug 07, 2006
Posts: 73
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

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


I love this place!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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 .


[Jess in Action][AskingGoodQuestions]
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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

Joined: Aug 07, 2006
Posts: 73
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Jan 09, 2008
Posts: 525
Campbell : Please don't resist . . .

Jim ... ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another question about using comparator