• Post Reply Bookmark Topic Watch Topic
  • New Topic

compareTo with generic objects!  RSS feed

 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I want to create a class that can compare generic objects based on the natural order
for example, If I pass a String objects, it will arrange them based on the alphabetical order.

Here is my code for the class

[Added code tags - see UseCodeTags for details]


Can you please help me!
Why doesn't the compareTo method work with type T?
so that when I pass either Integer or String objects, it will sort them based on the natural order!


Thanks,
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method compareTo doesn't work with type T because not all classes have compareTo method (and T stands for "any class").
Compiler won't allow you to put value.compareTo in your code because value might not have compareTo method.
You need to make compiler sure that T will have compareTo method. That means it will implement Comparable<T>.
To specify that generic type implements (or extends) something (in this case Comparable<T>) you write:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mona Alsh wrote:I want to create a class that can compare generic objects based on the natural order

Well, other than the "based on the natural order" - which I hope Pawel's excellent post has explained - you're kind of re-inventing the wheel.

There is already an interface called Comparator (java.util.Comparator ←click) that covers what you want, even for types that don't have a "natural" order.

However, I think I know what you're trying to do - tell me if I'm wrong:
You want to create a generic "entry" that orders any Comparable (as Pawel explained) object in its natural order, so that you can then pass that entry to a single List or Map wrapper, or to a "generic" sort method.

Am I close?

If not, don't bother reading any further unless you're interested.
If I am, then I'm afraid you're slightly off-base - but not that far. And your thinking is good.

First: why are you off-base?
Answer: because any class that is Comparable already has a natural order, so you can simply supply it as a type to any ordered collection (like TreeSet), or a sort method like Collections.sort(), and the natural order will automatically be used.

BUT (and here's where your thinking is definitely on the right lines): what if I want to create a generic ordered collection where I don't know if the supplied type is Comparable or not?

Comparator to the rescue.

Both Collections.sort() and ordered collections like TreeSet take an optional Comparator, so why not just supply it all the time? Very straightforward.

So now your problem becomes: how do I create a generic Comparator for a Comparable that replicates its "natural order"?

Pawel has already given you the start (T extends Comparable), but I'll let you have a try before I offer any more help.

Suffice to say: I've already written exactly such a class, and I called it NaturalOrder. And it's very useful.

If you have any problems writing it, come back and I'll try and guide you there.

Winston
 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Method compareTo doesn't work with type T because not all classes have compareTo method (and T stands for "any class").
Compiler won't allow you to put value.compareTo in your code because value might not have compareTo method.
You need to make compiler sure that T will have compareTo method. That means it will implement Comparable<T>.
To specify that generic type implements (or extends) something (in this case Comparable<T>) you write:


Thanks Paul!
I thought that if I pass a type that is comparable, then it'll arrange it in natural order and if not then only now the compiler should complain.
But as you pointed out, Java compiler needs to be sure that the compareTo is applicable.
 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mona Alsh wrote:I want to create a class that can compare generic objects based on the natural order

Well, other than the "based on the natural order" - which I hope Pawel's excellent post has explained - you're kind of re-inventing the wheel.


Winston


I would definitely want to create a generic comparator but I think I'll need to be more expert, so I fixed the problem by just
converting the input value to a String so that it'll order based on the String natural order.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!