Created
November 17, 2011 20:27
-
-
Save stephenmcd/1374413 to your computer and use it in GitHub Desktop.
Sort object list by index of attribute in a keylist
This file contains hidden or 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 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