This week's book giveaway is in the Android forum.
We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!
See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Comparator interface  RSS feed

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,


A Comparator must define a compare function which takes two Objects and returns a -1, 0, or 1.

Here what happens internally, when the compare method returns -1 or 0 or 1.


Thanks,
Praneeth.
 
Sheriff
Posts: 23714
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

praneeth ram wrote:A Comparator must define a compare function which takes two Objects and returns a -1, 0, or 1.



Well, no. It should return a negative number, zero, or a positive number.

Here what happens internally, when the compare method returns -1 or 0 or 1.



What happens "internally"? Some code like this:

But obviously you had some other question, since that's trivially obvious. Could you clarify?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compare method returns a int which can be a negative,positive or a 0.
Example: class Example implements comparator {
public int compare(Object A,Object B) {
return ObjA.compareTo(Objb);
}
}

If 0 is returned ObjA = Objb
If -ve is returned ObjA < ObjB
If +ve is returned ObjA > ObjB
So it actually uses a compareTo method internally for sorting.The comparator interface is used if you would want to sort a collection in multiple ways.

Joseph
 
praneeth ram
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not satisfied with your reply. we can easily understand obj1<obj2 returns positive, greter than mean +ve, equal mean zero....nothig is great here.

who are going to use these +ve -ve zero values, where these can be useful, and how it works,

thanks,
praneeth.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can be useful if you want to sort the objects. Sort algorithms generally work by comparing pairs of objects to see what order they should be in (the clever parts of the algorithm are deciding which objects to compare and what to do then - e.g. a bubble sort compares adjacent pairs and swaps them if they're in the wrong order, until there are no more to swap). So all they need is a method to tell them how to compare two objects - that's what Comparator (and Comparable) supplies.

 
Joseph Arnold
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who will use? Anyone who wants to sort a collection. It is used implicitly if the objects are in a TreeSet where you MUST implement comparable interface for the objects. How does the system compare? For numeric variables then sorting is obvious, if they are characters, they will be sorted by their ASCII codes. Hope I have answered your question clear enough.

Joseph
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!