Re: flexibility in sort compare functions

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Tue Feb 10 1998 - 19:52:31 MET


Hi George,

   the quick way out would be to implement a Compare() function for
the objects you store in the TClonesArray that uses a user settable
function to do the actual compare. Something like:

class GIObject : public TOBject {
private:
   ...
   static Int_t (*fgCompare)(GIObject *, GIObject *);
public:
   void SetCompareFunc(Int_t (*comp)(GIObject *, GIObject *))
          { fgCompare = comp; }
   Bool_t IsSortable() const { return kTRUE; }
   Int_t  Compare(TObject *obj);
};

GIObject::Compare(TObject *obj)
{
   return (*fgCompare)(this, obj);
}


Now before doing the sort you do GIObject::SetCompareFunc() to
the function you want.

Hope this helps.

Cheers, Fons.



George Irwin wrote:
> 
> I need the ability to sort a TClonesArray with various compare
> functions.  For example, sort on plane number, or counter number
> within plane, or pulse height, or time, or some combination of these,
> etc.  TClonesArray seems to want me to compile my compare function
> into the object occupying the TClonesArray.  But I need the
> flexibility to define new kinds of sorts in an analysis job
> constructed after the object in the TClonesArray is defined.
> With STL containers I would use the STL sort construct with different
> externally defined compare functions.  How can I achieve this
> functionality with TClonesArray.  Thanks.  George

-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland          Phone: +41 22 7679248
E-Mail: Fons.Rademakers@cern.ch              Fax:   +41 22 7677910



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:30 MET