filbert icon indicating copy to clipboard operation
filbert copied to clipboard

No support for sort keys?

Open danieltanfh95 opened this issue 11 years ago • 3 comments

a=["a","aa"]
a.sort(key=len)
print(a)

This returns an error that key is not a defined keyword argument: SyntaxError: Unexpected token TypeError: Cannot read property 'type' of undefined

danieltanfh95 avatar Jul 15 '14 09:07 danieltanfh95

This is not supported yet.

#26 is tracking adding the sort method to lists #8 is tracking adding keyword arguments

Here is a workaround:

def cmp(a, b): return len(a) - len(b)
a=["ccc", "a","aa"]
a = sorted(a, cmp)
print(a)

To see it in action, you can paste it here: https://rawgit.com/differentmatt/filbert/master/test/interactive.html

Thanks for the feedback!

differentmatt avatar Jul 15 '14 16:07 differentmatt

I just wanted to make a comment regarding your workaround...

cmp is for comparisons of two items. in this case, a and b.

if cmp returns a negative, that means a is less than b. if it returns zero, they are equal, and if it returns a positive value, greater than

so differentmatt, your code is wrong. here's what it should be:

def cmp(a, b): return len(a) - len(b)
a=["ccc", "a","aa"]
a = sorted(a, cmp)
print(a)

sp1d3rx avatar Aug 25 '14 19:08 sp1d3rx

Good catch, updated comment.

differentmatt avatar Aug 25 '14 20:08 differentmatt