Change the order of entries in a TLegend

Hi,

In a script I add entries to a TLegend in a loop with AddEntry(), and now I would like to change the order of the entries.

Especially I would like to have an entry to be the first one in the legend.

Is there a way to set the place of an entry?

I looked in the TLegend class description but I can’t figure out how I can do that.

Thanks a lot! :slight_smile:

Ric.

The simplest would be to Add them in the right order. Is it possible to do that in your script ?

Actually in my script I fill a python dictionary and then I loop over dictionary keys, and entries in dictionaries are not sorted. So it’s difficult to adding entries in a specified order.

Following your other answer I managed building a different TLegend with just the entry I want to be showed as first.

Thanks!

Ric.

Ric,

starting with python2.4, there is the “sorted” builtin function that takes an iterable sequence and turns it into a sorted list. For your dictionary example, that would be “sorted(myDict)” which will return a sorted list of the keys. Then, you can write code like:

for key in sorted(myDict): print key, myDict[key]etc. Note that because of the use of a builtin (i.e. written in C) makes it fast, and the use of the iterator protocol makes it memory efficient. Thus, it should work fine even for large dictionaries.

HTH,
Wim