aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comparator" Watch "Comparator" New topic
Author

Comparator

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

It is said that ,
The Comparator interface gives you the capability to sort a given collection any number of different ways


What i understood from this is,.
if i have a class
class A implements Comparator
{
int i;
String s;
}

The objects of the class can be compared by i and also by s

Correct me if i am wrong?/




SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

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))


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

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 ! !
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

You can have two Comparator implementations (one uses int and other uses String value of your class) then use those two implementations where you need the collection sorted by each different way.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

ok.. suppose i have one comparator as


this one is comparing based on string variable of the object.
And the below one is comparing based on int value of the object (assuming the method returns a Integer object)



but how do i put that in the same class??
Doesn't that give a error saying that there is two methods with same signature???
Mustafa Musaji
Ranch Hand

Joined: May 03, 2008
Posts: 52
What you need to do is create two comparators using implements Comparator. This will force you to implement the method compare(Object one, Object two). This returns an int.

In your main class, when you have your array of objects you want to sort in a particular order you can do Arrays.sort(your array, new ComparatorName);

If you do a search for Java Comparator Example in Google you'll get exactly what you're after, but I hope this helps either way.


SCJP 5.0 - Passed
Mustafa Musaji
Ranch Hand

Joined: May 03, 2008
Posts: 52
I was bored....

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@ Mustafa Musaji,

Thanks man, this was what i was looking for. I was actually thing that the comparator's method would be overloaded in the same class. i mean like something this .

class x implements comparator<...>
{
// overloaded method for comparing strings
//overloaded method for comparing ints,
}

NOw i realize that its kind of foolish thing( not possible).


@Vijitha Kumara,
Now your post makes a perfect sense for me.

Thanks guys.




James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Anyother question , Does the Collections.sort() takes only subclass of list inteface???
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

James Tharakan wrote:Anyother question , Does the Collections.sort() takes only subclass of list inteface???


Yes! To sort an array you have to use Arrays.sort and to sort Sets, you have to use TreeSet


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

okay.... thanks man ..
i was wasting time try to figure out how to sort a set using Collections.sort
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

class person{
int age;
String name;
}


How do i use comparator in case of using Treeset, in order to sort in different ways?

Which is the metod can i use?? Is is possible?
I am able to work with only comparable interface.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

You can use Comparator with a TreeSet just as you use Comparator with Collections.sort.

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Okay ... i never used such a syntax...

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??
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


SCJP 6
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@ punit
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());
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

James I think what you are asking is only possible by creating a new TreeSet from the existing tree set like this

TreeSet<Person> set1 = new TreeSet<Person>(new Comparator1<Person>());
//add elements to set
TreeSet<Person> set2 = new TreeSet<Person>(new Comparator2<Person>());
set2.addAll(set1);

Posted by Punit:

mySet.add(new Person("Ankit",22));


Hey I am 20 not 22
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

hmm.... thanks man
Abhishek Bhat
Greenhorn

Joined: Jan 01, 2009
Posts: 25
#
#


Guys,

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 ?
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

What you have understood is correct.
Abhishek Bhat
Greenhorn

Joined: Jan 01, 2009
Posts: 25

Thanks James !
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparator