Skip to content

Instantly share code, notes, and snippets.

@pelletier
Created July 22, 2011 13:56
Show Gist options
  • Save pelletier/1099501 to your computer and use it in GitHub Desktop.
Save pelletier/1099501 to your computer and use it in GitHub Desktop.
def sort_bug(in_list):
"""
>>> sort_bug([('bar', '...'), ('bar/baz/bob', '...'), ('bar/baz', '...'), ('foo', '...'), ('foo/bar', '...'), ('a', '...'),])
[('foo/bar', '...'), ('foo', '...'), ('bar/baz/bob', '...'), ('bar/baz', '...'), ('bar', '...'), ('a', '...')]
>>> sort_bug([('bar', '...'), ('bar/baz', '...') , ('bar/baz/bob', '...'), ('foo', '...'), ('foo/bar', '...'), ('a', '...'),])
[('foo/bar', '...'), ('foo', '...'), ('bar/baz/bob', '...'), ('bar/baz', '...'), ('bar', '...'), ('a', '...')]
"""
return sorted(in_list, cmp=lambda a,b: 1 if a[0] <= b[0] else -1)
import doctest
doctest.testmod()
@nautilebleu
Copy link

OK I understand: I get the list from a remote server as ['bar', 'bar/baz', 'bar/baz/bob', 'foo', 'foo/bar'] and not ['bar', 'bar/baz/bob', 'bar/baz', 'foo', 'foo/bar'] so this limitation is not a problem for me.

@brunobord
Copy link

see my fork for better comparison (I may be wrong, though)

https://gist.github.com/1099506

@nautilebleu
Copy link

nautilebleu commented Jul 22, 2011 via email

@pelletier
Copy link
Author

Désolé pour la forme criptique : j'ai un faible pour les lambda :)

@nautilebleu
Copy link

nautilebleu commented Jul 22, 2011 via email

@pelletier
Copy link
Author

Ah d'ailleurs, si a et b sont des tuples, ca change un peu. Cf https://gist.github.com/1099501/0e675e9c708b924079556c313d0ed574e6b7350f

(Edit: note que ca marche a priori pour n'importe quelle profondeur de chemin, et dans n'importe quel ordre)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment