Skip to content

Instantly share code, notes, and snippets.

@karlcow
Created March 10, 2021 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlcow/6330cfe7fff30baf1e8d999ff49819fa to your computer and use it in GitHub Desktop.
Save karlcow/6330cfe7fff30baf1e8d999ff49819fa to your computer and use it in GitHub Desktop.
products = """A,10,20
B,50,30
C,10,10
D,50,40
E,60,10"""
product_lines = [product.split(',') for product in products.splitlines()]
# sorting by INCREASING price
per_price = sorted(product_lines, key = lambda product: product[2])
# [['C', '10', '10'], ['E', '60', '10'], ['A', '10', '20'], ['B', '50', '30'], ['D', '50', '40']]
# Then sorting according to the second key decreasing, the price being already ordered will stay sorted.
per_popularity = sorted(per_price, key = lambda product: product[1], reverse=True)
# [['E', '60', '10'], ['B', '50', '30'], ['D', '50', '40'], ['C', '10', '10'], ['A', '10', '20']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment