Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Comparator interface

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Squanch that. And squanch this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic