The method described above works for Python 2.5 but not for 2.4.4. Operator.itemgetter only accepts one argument under 2.4.4. Another problem I found was that with 2.5 the * will only flatten a sequence. So if you want to sort by unordered a set of unordered indices (e.g. -1, 4,5,6) your out of luck (from want I tried at least).
I ended writing a function which works with 2.4 and 2.5. I am posting it here encase someone runs into the same problem and happens to google this in the future.
-
def SortKeyByIndices(keylist,indices):
-
sortedkeys=sorted(keylist,key=operator.itemgetter(indices[0]))
-
if len(indices)>1:
-
teeth=[]
-
teeth.append([sortedkeys[0][indices[0]],0])
-
for ikey, key in enumerate(sortedkeys):
-
if key[indices[0]]!=teeth[-1][0]:
-
teeth.append([key[indices[0]],ikey])
-
teeth.append([teeth[-1][0],len(sortedkeys)])
-
for tnum, tooth in enumerate(teeth[:-1]):
-
subkeys=sortedkeys[teeth[tnum][1]:teeth[tnum+1][1]]
-
subkeys=SortKeyByIndices(subkeys,indices[1:])
-
sortedkeys[teeth[tnum][1]:teeth[tnum+1][1]]=subkeys
-
return sortedkeys
-
Usage
-
mykeys=mylist.keys()
-
mykeyssorted=SortKeyByIndices(mykeys,[1,2,5,6])
-