Created
March 10, 2021 05:40
-
-
Save karlcow/6330cfe7fff30baf1e8d999ff49819fa to your computer and use it in GitHub Desktop.
This file contains 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
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