• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Comparator

 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?/



 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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))
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mustafa Musaji
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was bored....

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyother question , Does the Collections.sort() takes only subclass of list inteface???
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay.... thanks man ..
i was wasting time try to figure out how to sort a set using Collections.sort
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use Comparator with a TreeSet just as you use Comparator with Collections.sort.

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm.... thanks man
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#
#


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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you have understood is correct.
 
Abhishek Bhat
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks James !
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic