What is the difference between comparable and comparator interface and in which condition which one should use
-
http://stackoverflow.com/questions/1440134/java-what-is-the-difference-between-implementing-comparable-and-comparator – BobTheBuilder Mar 04 '13 at 15:58
-
http://stackoverflow.com/questions/420223/what-is-the-difference-between-compare-and-compareto – BobTheBuilder Mar 04 '13 at 15:58
2 Answers
If you own sorting objects, you have a choice to implement Comapreble in them or separate logic into distinct Comparator. If you are working with third party objects and you do not want to extend them to add coparison logic, you use Comparator.

- 4,175
- 15
- 31
A Comparable is the interface that defines the natural ordering of objects. For example, String implements Comparable according to the lexicographic order, and Integers implement Comparable according to the numeric order. If a class is comparable, this will be the default order to apply on its instances (e.g. in binary search, sorting or SortedMap).
A comparator defines an independent comparison mechanism, which can sometimes be an alternative to the natural ordering of elements. For example, you can use a different ordering logic for strings, and use it for sorting string arrays.

- 22,166
- 5
- 47
- 78
-
Complete article at : http://techgyani.co.in/java/difference-between-comparable-and-comparator/ – shatakshi Jan 20 '19 at 11:46