# this customize sort did not work ,what's wrong?

MRAB python at mrabarnett.plus.com
Sun Jan 24 00:10:00 CET 2010

```thinke365 wrote:
> jesus, now i fixed it, using odd lambda sort.
> l.sort(lambda x,y: cmp(len(x), len(y)))
> print l
>
> BUT I AM STILL CONFUSED WHY COSTOMIZED SORT FAILED TO SORT AS IT IS
> PROGRAMMER!
>
>
> thinke365 wrote:
>> i mean the output i want is:
>> [ [1, 2], [1, 2, 3, 4], [1, 2, 3, 4, 5]], that is sort according to the
>> length of the list element
>>
>> thinke365 wrote:
>>> l = list()
>>> l1 = list((1, 2, 3, 4))
>>> l2 = list((1,2))
>>> l3 = list((1, 2, 3, 4, 5))
>>> l.append(l1)
>>> l.append(l2)
>>> l.append(l3)
>>> print l
>>>
>>> def sort_by_list(E1, E2):
>>>     print len(E1), len(E2)
>>>     return len(list(E1)) > len(list(E2))
>>>
>>> l.sort(cmp=sort_by_list)
>>> print l
>>>
>>> output:
>>> [[1, 2, 3, 4], [1, 2], [1, 2, 3, 4, 5]]
>>> 2 4
>>> 5 2
>>> [[1, 2, 3, 4], [1, 2], [1, 2, 3, 4, 5]]
>>>
>>> the order of the elements in the list did not change!
>>>
When comparing [1, 2] with [1, 2, 3, 4] you want it to return -1 to say
that it's shorter, so they should therefore be swapped, but you're
returning 0 (False), which says it's the same length, so they're not
swapped.

This is also works:

l.sort(lambda x,y: len(x) - len(y))

because the sort really only looks at the sign.

```