0 votes
in JAVA by
When to use comparable and comparator in java with example?

1 Answer

0 votes
by

When to use comparable and comparator in java with example?

In case one wants a different sorting order then he can implement comparator and define its own way of comparing two instances. If sorting of objects needs to be based on natural order then use Comparable whereas if your sorting needs to be done on attributes of different objects, then use Comparator in Java.

Related questions

0 votes
asked Feb 8, 2020 in JAVA by rahuljain1
+3 votes
asked May 31, 2020 in JAVA by SakshiSharma
...