Quantcast
Channel: When should I use Comparator vs Comparable? - Stack Overflow
Browsing index pages (3 articles)
↧

When should I use Comparator vs Comparable?

I have a list of POJOs I need to sort somehow. I define a Comprator inside the POJO class and use it to sort the list.Is the following way correct/best practice? Is there a better way to do it? public...

View Article


Answer by Alexander Ivanchenko for When should I use Comparator vs Comparable?

There's a substantial distinction between the use cases for Comparator and Comparable.Implementing the Comparable interface is suitable for objects that have a natural order in your domain model. I'm...

View Article


Answer by Bruno Grieder for When should I use Comparator vs Comparable?

This is not opinion based: TL;DR implement Comparable:semantically, this is what Interfaces are designed for: they express a contract enforced by an object, a behavior of the object: if the objects are...

View Article
Browsing index pages (3 articles)


Latest Images