Feb-06-2017, 05:26 PM
cmp is is removed in Python 3.
But lambda is really nice with sorting:
Sorting HOW TO
Quote:The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported.So it was a bad idea,and
key=somthing
is better and works also fine in Python 2.7.Quote:and I would prefer a way without lambda, if that's possible. I seem to remember that a function could be used?You can import
itemgetter
to avoid lambda.But lambda is really nice with sorting:
>>> lst = [('Tom', 'C', 10), ('Kent', 'A', 12), ('Jenny', 'B', 15)] >>> sorted(lst, key=lambda tup: tup[1]) [('Kent', 'A', 12), ('Jenny', 'B', 15), ('Tom', 'C', 10)]Sorting middle elements to A,B,C.
Sorting HOW TO