Skip to content

Instantly share code, notes, and snippets.

@stephenmcd
Created November 17, 2011 20:27
Show Gist options
  • Save stephenmcd/1374413 to your computer and use it in GitHub Desktop.
Save stephenmcd/1374413 to your computer and use it in GitHub Desktop.
Sort object list by index of attribute in a keylist
def sort_by_keylist(objects, attr, keylist):
indexes = dict([(x, i) for i, x in enumerate(keylist)])
return sorted(objects, key=lambda x: indexes.get(getattr(x, attr)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment