I am using Python 3.2 and would like to sort a list of tuples based on a configuration file:
formats=CCC;aaa;BBB
providers=yy;QQ;TT
Each tuple contains this information:
( title, size, format, provider )
I would like this group of tuples to first be sorted by the providers list. All yy's come before QQ's and TT's.
Then, keeping this result order, move onto formats. All CCC's before aaa's before BBB's.
Finally, the third criteria would be to sort by size (float), in decending order.
It is critical that each step uses a stable sort so that the secondary sort keeps the ordering of the first sort and so on.
How can I do this in a pythonic way? Thanks.
EDIT 1
This is what I tried, my it will obviously not work because of sorted(mydata). mydata can't be a list in this context.
providers="yy;QQ;TT"
formats="CCC;aaa;BBB"
p_dict = {}
f_dict = {}
for k,v in enumerate(providers.split(';')):
p_dict[k] = v
for k,v in enumerate(formats.split(';')):
f_dict[k] = v
mydata = (
('title1', 423.4, 'QQ', 'aaa'),
('title2', 523.2, 'TT', 'CCC'),
('title3', 389.0, 'yy', 'aaa'),
('title4', 503.2, 'QQ', 'BBB') )
sort1 = sorted( mydata, key=p_dict.__getitem__)
print(sort1)