Forums Register Login

another question about using comparator

+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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 ... ...
+Pie Number of slices to send: Send
 

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 .
+Pie Number of slices to send: Send
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 ... ...
+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I'll resist the temptation to confuse things even more by showing people how to write their Comparators as anonymous classes
+Pie Number of slices to send: Send
Campbell : Please don't resist . . .

Jim ... ...
How do they get the deer to cross at the signs? Or to read this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1267 times.
Similar Threads
determine the input type of the value of the function parameter map
Sorting ArrayList
sorting using comparator interface
Sorting by 3 variables dynamically in arrayList
Numeric Range Overlapping Strategy needed.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:18:49.