Hi JKid, Ok it works like this ... How would you compare your String / Integer objects > ?
There is a "Natural Ordering" which means if the List consists of String elements, it will be sorted into alphabetical order. If it consists of Date elements, it will be sorted into chronological order. How does this happen? String and Date both implement the Comparable interface. Comparable implementations provide a natural ordering for a class, which allows objects of that class to be sorted automatically. "Natural Ordering" doesnt work on the objects that you create , until you implement the Comparable Interface like what String or Integer classes does for "Natural Ordering" to happen.An other way is to have a Comparator Class that would help you in comparing your objects , on the criteria you decide.In the example that I send you I choose Date as the criteria.I would have written the Comparator in a separate class and used that Class say like EmployeeSortbyDate.Instead like in the tutorial I wrote a Anonymous class with no name and Instansiated this Class to the reference SENIORITY_ORDER of type Comparator. Now you can use this Comparator to sort the Employee type according to Date.And as far as calling is considered Collections.sort() is the method used for sorting collection. sort(List<T> list, Comparator<? super T> c) - Sorts the specified list according to the order induced by the specified comparator. This is the method used in the tutorial.So if you see the code you can see we use sort(employees , SENIORITY_ORDER) where employees is the list and SENIORITY_ORDER is the Comparator Object. Let me know if you have some doubts on this .. Always happy to help you out... Thanks, Ashok A V On Fri, Aug 7, 2009 at 2:28 AM, JKid314159<happy27...@yahoo.com> wrote: > Hi Ashok: > > Thanks for the files. I need to read again about comparators. I am not too > sure when the method call goes into the comparator class and how the > parameters are passed. > Thank you. > Respects, > > JKid314159 > http://existentialists.blogspot.com/ > > > > > --- On Thu, 8/6/09, Ashok A V <ashok.f...@gmail.com> wrote: > > From: Ashok A V <ashok.f...@gmail.com> > Subject: [java programming] Re: Lab1016-Comparator > To: happy27...@yahoo.com > Cc: javaprogrammingwithpassion@googlegroups.com > Date: Thursday, August 6, 2009, 4:04 PM > > Hi Jkid, > > I have attached three files with the mail which will help explain you > the Comparator behaviour > > Employee.java -- Bean for storing Employee data > Name.java -- Name class used in sun tutorial > TestEmployee.java -- A test class explaining the comparator code.(Here > the comparison of Employee objects is done by using the date) > > I have used only date , but you can use multiple criteria like number > .. try it yourself > > Thanks, > Ashok A V > > > On Thu, Aug 6, 2009 at 9:51 PM, JKid314159<happy27...@yahoo.com> wrote: >> Re: >> The Java Tutorials: >> Home Page > Collections > Interfaces > Object Ordering: >> Comparators >> >> Dear Java Programmer: >> I got lost on this. Can someone help me? Please comment. Thank you. >> this final Comparator Seniority is a variable? >> >> The ternary operator is doing??? There condition and then int or another >> condition to test which is also a ternary operator? >> >> static final Comparator<Employee> SENIORITY_ORDER = >> new Comparator<Employee>() { >> public int compare(Employee e1, Employee e2) { >> int dateCmp = e2.hireDate().compareTo(e1.hireDate()); >> if (dateCmp != 0) >> return dateCmp; >> return (e1.number() < e2.number() ? -1 : >> (e1.number() == e2.number() ? 0 : 1)); >> } >> }; >> >> >> Respects, >> >> JKid314159 >> http://existentialists.blogspot.com/ >> >> >> >> >> > >> > > > > -- > Victory belongs to the most persevering. > - Napoleon > > > > > -- Victory belongs to the most persevering. - Napoleon --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to javaprogrammingwithpassion@googlegroups.com To unsubscribe from this group, send email to javaprogrammingwithpassion-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en -~----------~----~----~----~------~----~------~--~---