...
> I was reading the following part from a tutorial.
Where? This one?
<http://www.cs.rit.edu/~tmh/courses/allJava/Anonymous.html>
Andrew T.
Shawn - 13 Sep 2006 13:47 GMT
> ...
>> I was reading the following part from a tutorial.
[quoted text clipped - 3 lines]
>
> Andrew T.
Yes. My question is not related to ananymous class concept, though. I
followed that part.
Babu Kalakrishnan - 13 Sep 2006 14:04 GMT
>> ...
>>
[quoted text clipped - 7 lines]
> Yes. My question is not related to ananymous class concept, though. I
> followed that part.
The sort method would use the specified comparator in order to figure
out the ordering of the sort. So when you're writing your own
comparator, you need to implement the compare method in such a way that
if you are handed two arbitrary members "o1" and "o2" from the list,
your method returns the correct result as apecified in the documentation
of the Comparator interface - i.e return a negative number if o1 should
appear before o2 in the ordering, a positive number if o1 should come
after o2, and zero if the two objects are deemed to be equal.
The sort implementation will call this method as many number of times as
is needed with different members of your list as arguments, and if you
return the correct result each time, the list would end up sorted in the
desired manner.
BK
> Hi,
>
[quoted text clipped - 24 lines]
> } // end of Comparator implementation
> ); // closed paren for sort() and end of statement semicolon
I guess the code you want filled in is "implementation for compare()".
Let's say you want to sort instances of the class Person by their last name,
and then by their first name. The code might look something like this
(didn't check it in a compiler, may contain typos):
<code>
class Person {
public String firstName, lastName;
}
new Comparator<Person>() {
public int compare(Person o1, Person o2) {
int lastNameCompareResult = o1.lastName.compareTo(o2.lastName);
if (lastNameCompareResult != 0) {
return lastNameCompareResult;
}
return o1.firstName.compareTo(o1.firstName);
}
}
</code>
if you don't understand how generics in 1.5 work yet, then equivalent code
in 1.4 would look like:
<code>
class Person {
public String firstName, lastName;
}
new Comparator() {
public int compare(Object t1, Object t2) {
Person o1 = (Person)t1;
Person o2 = (Person)t2;
int lastNameCompareResult = o1.lastName.compareTo(o2.lastName);
if (lastNameCompareResult != 0) {
return lastNameCompareResult;
}
return o1.firstName.compareTo(o1.firstName);
}
}
</code>
- Oliver
Shawn - 13 Sep 2006 16:29 GMT
> I guess the code you want filled in is "implementation for compare()".
>
[quoted text clipped - 40 lines]
>
> - Oliver
Thank you very much. I feel much better now. But one point I am deeply
confused in your code is:
int lastNameCompareResult = o1.lastName.compareTo(o2.lastName); //3
possible results: negative, 0, positive
if (lastNameCompareResult != 0) {
return lastNameCompareResult;
}
return o1.firstName.compareTo(o1.firstName); //which is zero
Why check if lastNameCompareResult is not 0? Why not just return it?
Thank you again.
Patricia Shanahan - 13 Sep 2006 16:33 GMT
>> I guess the code you want filled in is "implementation for compare()".
>>
[quoted text clipped - 52 lines]
>
> Why check if lastNameCompareResult is not 0? Why not just return it?
Suppose the two names are "John Smith" and "Jack Smith". The two last
names are equal, so lastNameCompareResult is 0. However, the names are
not equal, and can be ordered based on the first name comparison.
The first name ordering is used if, and only if, the last names match.
Patricia
Tris Orendorff - 13 Sep 2006 19:36 GMT
>> Thank you very much. I feel much better now. But one point I am deeply
>> confused in your code is:
[quoted text clipped - 15 lines]
>
> Patricia
It would be more clear if the first-name-ordering code was fixed. It should be:
>> Thank you very much. I feel much better now. But one point I am deeply
>> confused in your code is:
[quoted text clipped - 15 lines]
>
> Patricia
It would be more clear if the first-name-ordering code was fixed. It should be:
return o1.firstName.compareTo(o2.firstName);

Signature
Sincerely,
Tris Orendorff
[Q: What kind of modem did Jimi Hendrix use?
A: A purple Hayes.]
o1.firstName.compareTo(o2.firstName);
--
Sincerely,
Tris Orendorff
[Q: What kind of modem did Jimi Hendrix use?
A: A purple Hayes.]
Oliver Wong - 13 Sep 2006 21:47 GMT
> It would be more clear if the first-name-ordering code was fixed. It
> should be:
>
> return o1.firstName.compareTo(o2.firstName);
Oops. You're right. That was one of the typos that made it into that
program.
- Oliver