differentmatt / filbert

JavaScript parser of Python
Other
133 stars 27 forks source link

No support for sort keys? #25

Open danieltanfh95 opened 10 years ago

danieltanfh95 commented 10 years ago
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

differentmatt commented 10 years ago

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!

sp1d3rx commented 10 years ago

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)
differentmatt commented 10 years ago

Good catch, updated comment.