Skip to content

Instantly share code, notes, and snippets.

@nautilebleu
Created July 22, 2011 12:55
Show Gist options
  • Save nautilebleu/1099394 to your computer and use it in GitHub Desktop.
Save nautilebleu/1099394 to your computer and use it in GitHub Desktop.
Custom sort of a files list
def _cmp(a, b):
if a[0].rfind(b[0]) == 0:
return -1
elif a[0].rfind(b[0]) == -1:
if a[0] < b[0] :
return -1
return 1
else:
return 0
# Reorder the remote_tree so files that belongs to a folder are before
# the parent folder. It helps when deleting a folder that contains files
# as files are deleted before the folder so the deletion of the folder
# occurs when the folder is empty.
# before: ['bar', 'bar/baz', 'foo', 'foo/bar']
# after: ['bar/baz', 'bar', 'foo/bar', 'foo']
remote_tree = OrderedDict(sorted(remote_tree.items(), cmp=_cmp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment