That is indeed one alternative. I think however that Comparable objects are
cleaner. It is evident by just looking at the code how they are compared.
Otherwise (with the Comparator approach), you need to pair objects with
comparators, and it's not always clear which comparator to use with each
object.

Also Comparators need access to fields of the object that you don't
necessarily want to expose. If the object implements its own compareTo, it
has access to all the internal fields that may affect the comparison (for
example, hash values).

Comparators however have an advantage - in that specific case I could create
two Comparators: (1) compares by the score and then by doc (2) compares by
the score only. That gives me some flexibility. But code-wise, I would still
need to create two additional classes, while in the Comparable approach I
could just extend ScoreDoc and override compareTo.

Shai

On Dec 6, 2007 12:01 PM, Michael Busch <[EMAIL PROTECTED]> wrote:

> Hi Shai,
>
> I think you don't have to subclass ScoreDoc. Can't you simply implement
> a Comparator and pass it in the data structure you need?
>
> E. g.:
> Arrays.sort(scoreDocs, new Comparator() {
>
> public int compare(Object o1, Object o2) {
>  ScoreDoc d1 = (ScoreDoc) o1;
>  ScoreDoc d2 = (ScoreDoc) o2;
>
>    if (d1.score == d2.score) {
>      return d1.doc - d2.doc;
>    }
>    return d1.score - d2.score < 0 ? -1 : 1;
>  }
> });
>
> -Michael
>
> Shai Erera wrote:
> > Hi
> >
> > Today ScoreDoc is not Comparable. That prevents applications that would
> like
> > to use it in Comparable data structures (such as priority queues), but
> still
> > use other Lucene's objects, like TopDocs, unless they create a
> > ComparableScoreDoc which extends ScoreDoc and implements Comparable. To
> make
> > ScoreDoc Comparable requires very minor changes:
> > - Add implements Comparable
> > - Add:
> >     public int compareTo(Object o) {
> >         ScoreDoc oScoreDoc = (ScoreDoc) o;
> >         float oScore = oScoreDoc.score;
> >         if (score == oScore) {
> >             return doc - oScoreDoc.doc;
> >         }
> >         return score - oScore < 0 ? -1 : 1;
> >     }
> >
> > If you agree to do it, I'm willing to open an issue and provide a patch.
> It
> > shouldn't affect any current Lucene implementations.
> >
> > Thanks,
> >
> > Shai Erera
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


-- 
Regards,

Shai Erera

Reply via email to