File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Comparator & Comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparator & Comparable" Watch "Comparator & Comparable" New topic

Comparator & Comparable

Arun Prasath
Ranch Hand

Joined: May 23, 2007
Posts: 80

Could anyone tell me what is the difference between Comparator & Comparable interfaces? when do we use it?
Joe Ess

Joined: Oct 29, 2001
Posts: 9168


This interface imposes a total ordering on the objects of each class that implements it.

A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order.

[ May 14, 2008: Message edited by: Joe Ess ]

[How To Ask Questions On JavaRanch]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45286
A similar question came up last year: here.
I agree. Here's the link:
subject: Comparator & Comparable
It's not a secret anymore!