Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No support for sort keys? #25

Open
danieltanfh95 opened this issue Jul 15, 2014 · 3 comments
Open

No support for sort keys? #25

danieltanfh95 opened this issue Jul 15, 2014 · 3 comments
Labels

Comments

@danieltanfh95
Copy link

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
Copy link
Owner

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
Copy link

sp1d3rx commented Aug 25, 2014

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
Copy link
Owner

Good catch, updated comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants