Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing objects of different classes derived from the same base

I have two classes, let's say DerivedOne and DerivedTwo, derived from a base class Base.

Assuming I have these:

DerivedOne d1;
DerivedTwo d2;

when I compare d1 and d2, I'd like d1 to always be smaller, in other words an object of DerivedOne should have a higher priority than an object of DerivedTwo. What is the best/nicest way to do that?

like image 392
Yunus Avatar asked Feb 20 '26 02:02

Yunus


1 Answers

I do not know if this is the nicest, but I would recommend using the instanceof operator inside the comparator.

like image 171
Matthew Avatar answered Feb 22 '26 16:02

Matthew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!