Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections

 
Muhammad Usman
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello to all,
while studying the collection class topic, i have come accross the term " comparator".
what does it mean,
can any body tell me.
thnaks
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you go to sort something - what order should in result in?
The thing that determines what comes first and what next etc., is the comparator. Any class which implements the Comparator interface can be used to compare things using the compare() method that is promised by the interface.
If you are just sorting numbers or characters, there is an obvious "natural order" to them, but if you are sorting objects that themselves may consist of lots of references to other objects - well it gets much more confusing.
In the end even the most complicated sort comes down to looking the objects two at a time and deciding which is first. A Comparator provides the rules for how to figure out which is first - or if they compare as exactly the same.
If you create a class which is a Comparator (it implements the Comparator interface), you have the opportunity to define the ordering rules for this class yourself. However, in the end you need to make sure that the basic rules of logic prevail. Two things should never compare as exactly the same unless you can use the .equals method on them and it will result in "true".
[ March 13, 2002: Message edited by: Cindy Glass ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic