I looked up the difference between Comparator and Comparable. I found this post http://forum.java.sun.com/thread.jspa?threadID=522388&messageID=2500053from Java's forum. In short, Comparable is used for a natural ordering of the objects. Comparator allows for custom comparisons of the same objects. In our example, ScoreDoc can implement Comparable and compare using the score only. That's a true ordering of ScoreDoc. If you don't like the second ordering (in case of tie in scores), we can offer that as static Comparators inside ScoreDoc. For example (from the forum), String implements Comparable and also includes several Comparator implementations. The Comparable implementation compares Strings by their ASCII, while it offers static String.CASE_INSENSITIVE_ORDERto compare the string in an insensitive order.
To conclude, by implementing Comparable on ScoreDoc, applications can use it as-is for natural ordering. I agree that the second ordering (by doc Id) may not fit all applications, so that can be omitted from compareTo. In addition, we can offer another Comparator: ScoreDoc.SCORE_THEN_DOC_ORDER (or something similar), or leave it out altogether. But having ScoreDoc implements Comparable can be a good service to many applications. And ... it doesn't break anything for existing applications or changes API. On Dec 8, 2007 8:55 AM, Chris Hostetter <[EMAIL PROTECTED]> wrote: > > : In general I would agree that people may want different implementations > for > : compare(), but I hardly see that's the case for ScoreDoc. After all, you > can > : either compare it by score or by doc (at least now). I believe that > since > : most people use the TopDocsHitCollector, they prefer the > compare-by-score > : approach ... > > sure, but that's not all your suggested compareTo does ... it first > compares by score and then does a secondary comparison by docId. > some people might want docs added "more recently" to sort first instead, > some might want docid left out of hte comparison all together. > > This is where Comparators are more useful then compareTo methods. We can > add *lots* of different static inner Comparator classes to ScoreDoc, but > if we add any compareTo method it could wind up burning someone down the > road. > > > -Hoss > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -- Regards, Shai Erera