How can I sort histograms in TList?

I would like to sort histograms inserted randomly in TList like this.

h5, h4, h1, h3, h2 -> h1, h2, h3, h4, h5.

All histograms in TList have the name of h+[number].

Do you have any idea?

Hi,
use TList::Sort.
Axel.

Hi,

TList::Sort is useful for just when the element of TList is pre-defined as sortable class.
It is not useful for user defined sort as our case.

Anyway, I found the solution using another TList object.

{
       ......
        HistBox = new TList;
       ......
        int nsize = HistBox->GetSize();
        int hid[nsize];

        for ( int i=0; i < nsize ; i++ )
        {
                TH1 *hist = HistBox->At(i);
                TString name = hist->GetName();
                name.Remove(0,1);
                hid[i] = name.Atoi();
        }

#include <algorithm>
        std::sort(hid, hid + nsize);

        HistBox2 = new TList;
        for ( int i=0; i < nsize; i++ )
        {
                TString name = TString("h") + hid[i];
                HistBox2->Add(HistBox->FindObject(name));
        }
        ......
}

Hi,

TList l; l.Add(new TH1F("h4","h",1,0.,1.)); l.Add(new TH1F("h1","h",1,0.,1.)); l.Add(new TH1F("h3","h",1,0.,1.)); l.Add(new TH1F("h2","h",1,0.,1.)); l.Sort(); l.First()->GetName(); works perfectly for me. Glad you found a solution.

Axel.

Hi,

The Sort of TList seems to sort the elements
in the dictionary order of alphanumeric.
It isn’t useful for the elements of TList
with the different numbers of digits in the name like this.

h135, h44 -> h44, h135

Sorry to present too simplified example in my question.
Anyway, I’m glad to learn the sorting algorithm of TList.