Skip to content

Instantly share code, notes, and snippets.

@voroninman
Last active September 10, 2017 09:39
Show Gist options
  • Save voroninman/72fae0bfa774ff54e382a236036b0624 to your computer and use it in GitHub Desktop.
Save voroninman/72fae0bfa774ff54e382a236036b0624 to your computer and use it in GitHub Desktop.
class Category(object):
def __init__(self, slug=None):
self.slug = slug
def __repr__(self):
return self.slug
categories = [
Category(slug='b'),
Category(slug='d'),
Category(slug='c'),
Category(slug='a'),
]
order = ['a', 'b', 'c']
categories = sorted(categories,
key=lambda c: order.index(c.slug) if c.slug in order else len(order))
print(categories)
# [a, b, c, d]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment