Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Can you help me understand this?

Thread view: 
Shawn - 13 Sep 2006 13:33 GMT
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
Andrew Thompson - 13 Sep 2006 13:41 GMT
...
> 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
Oliver Wong - 13 Sep 2006 15:13 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.