difference between comparabe and comparator


What are main diferences between comparabe and comparator.

what is purpose only two comparator implemented classes Collator, RulesBasesColator.(where as comparable implemented classes are a wrapper classes and String

Please advise
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Comparable is a property of the thing being sorted.  E.g. You might want to sort names alphabetically, so you could have a Person class that implements Comparable and sorts into alphabetical order based on the name property.

So you end up with:
Person implements Comparable { ... }

But the weakness of Comparable is that you can only have one type of sorting for those objects - because Comparable is a property of the object itself.

So say you also want to be able to sort a Person based on name or based on age.  You can't do that with Comparable, because you already defined Person as sorting by name.  So that's where Comparator comes in.  It's much more general.

That's 2 new classes, whose entire job is to just to compare objects - person objects in this case:
public class SortByName implements Comparator<Person> { ... }
public class SortByAge implements Comparator<Person> { ... }

You can write as many Comparators as you want and use them in sorting functions.

So if in doubt, Comparator is the more general concept.
Only use Comparable for what people call "the natural order", when you have something that only ever needs to be sorted in one way - like say a list of numbers, which you only ever sort in one way.

Make sense?


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
That is more clear.

what is purpose only two comparator implemented classes Collator, RulesBasesColator

Can you please advise on above two classes about which i never ever heard till last week
I've never used either Collator or RulesBasesCollator, but both are designed to help with sorting text.

That may sound like a trivial problem - putting words into alphabetical order.

But when you start to think about more complex character sets, with accents for example, it's not so simple to sort them correctly.  So a Collator is a class designed to help with that sort of sorting.

Unless you're interested in sorting text written in a range of different character sets it's not likely you need this.  You also don't need to understand them to understand how comparators work.

Yes, and I would say that the best way to eventually understand the differences between the two is to first understand fully how one of them works, starting with the Comparable interface - and your question on this is still open. Is it still unclear?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.