Sort a list with longest items first
问题 I am using a lambda to modify the behaviour of sort. sorted(list, key=lambda item:(item.lower(),len(item))) Sorting a list containing the elements A1,A2,A3,A,B1,B2,B3,B , the result is A,A1,A2,A3,B,B1,B2,B3 . My expected sorted list would be A1,A2,A3,A,B1,B2,B3,B . I've already tried to include the len(item) for sorting, which didn't work. How to modify the lambda so that the sort result is instead? 回答1: Here is one way to do it: >>> import functools >>> def cmp(s, t): 'Alter lexicographic