• 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 24081
54
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
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!