Created
July 22, 2011 12:55
-
-
Save nautilebleu/1099394 to your computer and use it in GitHub Desktop.
Custom sort of a files list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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