Skip to content

Instantly share code, notes, and snippets.

@dansteingart
Created January 15, 2014 04:37
Show Gist options
  • Select an option

  • Save dansteingart/8430827 to your computer and use it in GitHub Desktop.

Select an option

Save dansteingart/8430827 to your computer and use it in GitHub Desktop.
Sorting against an arbitrary standard
#Driven By Trello Card Sorting
#We want to preserve this order
color_list = ['green','yellow','orange','red','purple','blue']
dd = [
{'color':'yellow','name':'Jet'},
{'color':'blue','name':'Spike'},
{'color':'green','name':'Faye'},
{'color':'purple','name':'Ed'},
{'color':'orange','name':'Ein'},
{'color':'red','name':'Viscious'},
]
new_dd = sorted(dd,key=lambda k: color_list.index(k['color']))
for n in new_dd:
print n
# output
# {'color': 'green', 'name': 'Faye'}
# {'color': 'yellow', 'name': 'Jet'}
# {'color': 'orange', 'name': 'Ein'}
# {'color': 'red', 'name': 'Viscious'}
# {'color': 'purple', 'name': 'Ed'}
# {'color': 'blue', 'name': 'Spike'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment