Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
  • 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
  • salvin francis
  • Piet Souris


Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Question : Why do we use Comparator in Real time . in this code below the class what else can i do ? i mean                              

Posts: 23845
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Saad Zahoor wrote:

This is *not* a valid compare() method -- as required by the Comparator class. As described by the JavaDoc, a comparator needs to be transitive -- Or else it will produce different results depending on what elements, and/or the order the sort algorithm uses to compare those elements. Of course, the JavaDoc chosen to use some blah blah math explanation, but that is the jist of it...

Heck, even without the underlying requirement, you are not even following the requirement of the first paragraph as stated in the JavaDoc to be a valid comparator.

JavaDoc compare method wrote:
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Posts: 14489
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming that by "real time" you meant "practical, real-world situations"

Sometimes, natural ordering isn't what we need. Or maybe there is no intuitive natural ordering. If you have a collection of objects that you want to sort in different ways, you can use different comparators to do that, one comparator for each different sorting criteria.

Say you had some objects that had name, color, size, weight, and price attributes. To sort these with Collections.sort(List), the class needs to implement the Comparable interface, which defines the compareTo() method. This will be the class' natural ordering, which you can think of as its default ordering. Say you used the name attribute for natural ordering. So when you sort, these objects will ordered by name in alphabetical order.

What if you wanted to sort in reverse alphabetical order? By color? By weight? Any other way besides name in alphabetical order? If you wanted to use Collections.sort(), you'd have to use the overloaded version that takes a Comparator as its second parameter. The comparator basically encapsulates the logic for determining whether one object should be considered as coming before or after another object in a certain ordering.

See also the tutorial on ordering:
A timing clock, fuse wire, high explosives and a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!