In Python, I have something like the following (although shuffled randomly):
l = [('a', 'x'),
('a', 'y'),
('a', 'z'),
('b', 'x'),
('b', 'y'),
('b', 'z'),
]
If I call sorted(l), I get a sorted result (e.g. above), which is to be expected. However, I need to forward sorting by the first element of the tuple and reverse sorting by the second element. In other words, I would like to get the following result:
l = [('a', 'z'),
('a', 'y'),
('a', 'x'),
('b', 'z'),
('b', 'y'),
('b', 'x'),
]
Python2.x has a parameter cmpthat can be passed in sorted()to achieve this result, but Python3 no longer has this. It has only a parameter key. Is there a way to achieve the desired sort order using only a parameter key?
, - functools.cmp_to_key ( ), . ?
edit: , [. (basestring, datetime)].