Re: [ROOT] Sorting any TObject

From: Rene Brun (
Date: Wed Mar 10 2004 - 09:41:17 MET

Hi Lee,

The TObject::Compare method is dummy. It is supposed to be implemented
for classes that require a Sort algorithm. It is difficult to propose
a default implementation based on some hypothesis like the one you propose.

However, in your case, I think that there are two alternatives, simpler and
cheaper solutions.

1- if your fID is small, you can build directly a TObjArray (instead of TList)
where array[fID] points to your TGeotrack object.

2- if not, I suggest to create a dynamic array of integers with the size
corresponding to the number of elements in the TList, sort this array
with TMath::Sort and create a TObjArray where
array[i] points to the TGeoTrack with fID=index[i], index being the array
of indices sorted by TMath::Sort.

Rene Brun

"LEE, KERRY T. (JSC-SR) (UHCL)" wrote:
> Dear ROOT team,
> I would like to be able to sort any TList of objects that inherit from
> TObject using some common Int_t.  Say for example that I'd like to sort a
> currently unsortable list of objects such as TGeoTrack, by their fId number.
> If I could toggle the IsSortable flag, and then set the TObject::fUniqueID =
> fId, then I could sort any List of TObjects that I choose using a default
> Compare function in TObject, which sorts by fUniqueID.
> More specifically, I am trying to sort a list of TGeoTrack objects.  I read
> data for these tracks from a file and put the data into TGeoTrack objects,
> and then place the TGeoTrack objects in a TList in the order I read them in.
> This order is not always the order of Parent->daughter->granddaughter etc,
> but from the sequential track id numbers and parent id numbers this
> ancestral hierarchy can be determined.
> There is another way for me to do this, but it requires looking through the
> list each time for the next largest track id number, and this could
> potentially consume a lot of cpu time if the list contained a large number
> of objects.  Sorting the list once by track id solves this problem.  I could
> of course write my own sorting routine, but using the already optimized ROOT
> framework is more desireable.
> Would it be possible to implement this default sorting mechanism for any
> list of TObjects?
> Thanks
> Kerry

This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:06 MET