The objects of the class can be compared by i and also by s
No. Using Comparator over Comparable gives you different ways to sort a given collection. You can define any number of Comparators and uses them where you want to (you may use Collections.sort() and passes one of the comparators implemented by you along with the collection you want to use and sort method will sort the collection according to the criteria defined by you in the compare(T t,T t) method). Whereas using Comparable let you passes a collection (which contains the objects implementing the Comparable interface) to a sort method, but that way you can sort those objects only in one way (the way in which you have implemented the compareTo(T t))
Actually what i was thinking is ...
In a collection which is having the objects of class A....
Is it possile to sort as follows...
first time... sort the colletion based on object's string(i.e instance varible s)
second time .... sort the colection based on objects's int variable ( i.e instane varible i)
by using the comparator ???
Hope my question is clear and you understood what i am thinking of ! !
Now , i have created this tree set using this syntax,i mean , by sorting defined by PersonSorter1(String name).
if i want to sort this same treeset in differnt manner,i.e by ( int age).Then how would i do that??
The program that you gave will sort by age..
What i was actually looking for is , first sort by age and then sort by name also .. For the same TreeSet.
like , for a arraylist we can use Collections.sort.
In that we can define two class which implements comparator. one comparator sort will sort by name and the other will sort by age.
Is that possible for Treeset? ?
I thinking that we can do so, because we should specify that sorting method at the point of TreeSet declaration. by defining TreeSet<Person> mySet = new TreeSet<Person>(new PersonSorter1());
Do you need to use the "this" keyword in the method of the class that its been implemented in ?
I dont think you need it..please correct me if i am wrong guys...but i beleive...u need the this keyword only to differentiate between local and instance variables INSIDE the method its being dealt with.Ofcourse, this is used to tell the compiler that it refers to the class variable but when you perform an operation like the one above. you dont need to explicitly use the THIS keyword. Am i going wrong somewhere ?