Can you help me understand this?

S

Shawn

Hi,

I was reading the following part from a tutorial. I cannot understand
it. Could you kindly help me understand it? I know the intention is to
provide your own comparator so that your list can be sorted by using it.
I am not clear how to apply the object of Comparator to the list. Can
you fill in a little more code to show me how it works?

Thank you very much.


======================copied from a tutorial======================

This example demonstrates an anonymous class definition for a comparator
that is passed to the sort() method in the Collections class. Assume
that aList is a valid List of data that is to be sorted.

Collections.sort (aList,

new Comparator() { // implements the IF

public int compare (Object o1, Object o2 ) throws ..{

.... implementation for compare()

} // end of compare()

} // end of Comparator implementation
); // closed paren for sort() and end of statement semicolon
 
B

Babu Kalakrishnan

Shawn said:
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
 
O

Oliver Wong

Shawn said:
Hi,

I was reading the following part from a tutorial. I cannot understand it.
Could you kindly help me understand it? I know the intention is to provide
your own comparator so that your list can be sorted by using it. I am not
clear how to apply the object of Comparator to the list. Can you fill in a
little more code to show me how it works?

Thank you very much.


======================copied from a tutorial======================

This example demonstrates an anonymous class definition for a comparator
that is passed to the sort() method in the Collections class. Assume that
aList is a valid List of data that is to be sorted.

Collections.sort (aList,

new Comparator() { // implements the IF

public int compare (Object o1, Object o2 ) throws ..{

.... implementation for compare()

} // end of compare()

} // 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
 
S

Shawn

Oliver said:
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

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.
 
P

Patricia Shanahan

Shawn said:
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?

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
 
T

Tris Orendorff

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

It would be more clear if the first-name-ordering code was fixed. It should be:

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

It would be more clear if the first-name-ordering code was fixed. It should be:

return o1.firstName.compareTo(o2.firstName);

--
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.]
 
O

Oliver Wong

Tris Orendorff said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top