0

Possible Duplicate of

When should a class be Comparable and/or Comparator?

I understand the difference that is given in this link.
And also in the book that i am referring it is given that we can not use comparable when we need to sort the objects on the basis of more than one fields.

My Question:

I just want an example where we could not possibly use comparable and have to go for comparator in order to compare and please also show that with comparable why can't we compare on two different fields of object. If you find this question as duplicate please provide link,, i have searched many questions but none has the example that i wanted.

Community
  • 1
  • 1
sagar
  • 725
  • 2
  • 13
  • 30
  • 2
    `Comparator` is generally good for comparing a wide range of objects, such as sorting and even filtering and not all objects implement `Comparable`. `Comparable` is instance bound, you may want to change the way that a default `compare` method works, `Comparator` can make this simpler...but that's just me – MadProgrammer Sep 12 '14 at 04:50
  • You can always make a class Comparable instead of creating a Comparator -- you're right about that. Comparable is more restrictive, in that (a) you have to have control of the class (ie you can't make a class from a dependency Comparable), and (b) you can only implement Comparable once per class. – yshavit Sep 12 '14 at 04:50
  • 4
    Also, I don't know what the wording is in the book, but you can definitely implement `Comparable` such that you sort on the basis of more than one field. What you can't do is have it sort _in more than one way_. That is, you can say "always sort by last name, then first name" but you can't say "sort by last name" in one situation and "sort by first name" in another. For that, you need a `Comparator`. – yshavit Sep 12 '14 at 04:52
  • as i have mentioned ,, in **cay S horstman** it is given we can't use comparable when we have to compare objects on the basis of more than 1 field. Please give a code to explore this. – sagar Sep 12 '14 at 04:54
  • 1
    @yshavit as you have mentioned in your second comment, this is the exact wordings of the book.Please give an example to show where we actually stuck if use comparable and how comparator solve our problem. – sagar Sep 12 '14 at 04:56
  • I gave an example in my comment (comparing by last name in one context, and by first name in another). I would encourage you to try to solve that using a `Comparator`. If you write code that doesn't work as you expected, and have thought it through for a while and still can't figure it out, then you can open a new question. – yshavit Sep 12 '14 at 04:58

3 Answers3

1

If a class implements Comparable, this defines what is usually considered the natural ordering of it elements. In some cases this is the only ordering that may make sense, in other cases it might be the most widely used ordering. If you look for example at numbers, there is probably only one (total) ordering that makes sense (except maybe for taking the reverse). As others already have pointed out, their are other objects that have other useful orderings. What makes the primary ordering (or if there is even one) depends on your application. If you manage persons with adresses in you application, phonebook sort order could be considered the natural order if this is the most widely used one and sorting by age could be a secondary. Slightly OT: Beware of cases where non equal objects are considered equal wrt to the ordering, this may yield problems with containers like OrderedList etc.

Drunix
  • 3,313
  • 8
  • 28
  • 50
0

Comparing apples with each other will result in classes of equal apples, like red ones, green ones, old and fresh ones. That's OK as long as you only interested in a rather broad equality. But if you you are going to receive a paycheck you are very happy that you are identifiable within you equality class. So compareto is good for sorting and clustering and equals/hashcode is got for identification.

Hannes
  • 2,018
  • 25
  • 32
0

Comparable is mostly used when there is a 'known' default sort order and the object or class that we are ordering is editable or owned by the developer making the change.

Comparator is suitable where the class or object being ordered is not owned by the developer making the change like a web service response. It is also preferred when the natural ordering doesn't fit the objective that needs to be accomplished.

satish
  • 11
  • 1
  • 4