I am unfortunately having to use python with version before 2.4 So I don’t have the sorted built-in function. I need to sort a list; I found it is not possibly to go with
for aName in mylist.sort(lambda x,y:cmp(x.getName,y.getName)):
because then I would get an error saying
TypeError: iteration over non-sequence
I want to do the following:
for aName in sorted(mylist,key=lambda x:x.getName):
Can anybody help me with this? Many thanks.
You said that you wanted to do
for aName in sorted(mylist,key=lambda x:x.getName):but accepted an answer that did something else …It’s quite possible to write code that will run on multiple versions of Python 2.x. Here’s how to retrofit
sorted()functionality to Python 2.1 to 2.3. It uses the DSU (decorate-sort-undecorate) aka “Schwartzian Transform” method … see this section of the Sorting HOWTO but do read the whole HOWTO; it’s very informative.Running the above script with Python 2.7.1 and 2.1.3 produces the same output: