java.util.Comparator is used to define a special sort order for a class.
To implement java.util.Comparator you must write two methods. It typically describes a small delegate object passed to a sort to describe some special sort order. The compare method compares two objects of the class you want to sort. The equals method compares this java.util.Comparator to another to see if they represent the same order. It does not compare two objects of the class you want to sort. Normally you also redefine equals when you implement Comparator even though it is not part of the Comparator interface. There are no generics involved
public final int compare( Object o1, Object o2 ); public boolean equals( Object obj );
Here is a typical java.util.Comparator.compare routine:
If you are using a java.util.Comparator only once, you might implement it as an anonymous
Here is a very general purpose Comparator for sorting rows of a Table (array of arrays
Sorting ascending order means sorting with the small elements first then the big. This is usual ordering. Descending order means sorting with the big elements first then the small.
If you have a Comparator or Comparable of some kind, you can convert it into one that sorts into the reverse of the usual order, e. g. if the original sorts alphabetically, the new one will sort in reverse alphabetical order. Here is how you use it:
If you don’t have a suitable base Comparator, just write an ordinary Comparator from scratch and reverse the operands to each compare inside it, or return - result instead of result.
You don’t need any casts since the compiler checks that no non-Pair classes ever get inside the ArrayList<Pair>.
Sometimes you don’t know the types you are comparing at run time and generics just get in the way since they insist you know at compile time. It seems, no matter what you do, you get warning and error messages about generics when all you are trying to do is write code the way you did in Java version 1.4 before generics were invented. Here is a trick to get generics out the way, compliments of Mike Schilling:
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[22.214.171.124]
|Feedback||You are visitor number 125,811.|