Can anybody help me to sort the cards by suite and value? I have:
list_cards = [('♥', '9'), ('♥', 'J'), ('♦', 'J'), ('♥', '7'), ('♥', '10'), ('♦', '10')]
and I need to get:
[('♥', '7'), ('♥', '9'), ('♥', '10'),('♥', 'J'), ('♦', '10'), ('♦', 'J')]
I tried such methods:
return list_cards.sort(key=lambda c: (NAME_TO_VALUE[c[0]], c[1]))
return sorted(list_cards, key=lambda c: (NAME_TO_VALUE[c[0]], c[1]))
return sorted(list_cards)
and so on...
but the result differs from that I wanted to get. Here is the full code:
NOMINALS = ['7', '8', '9', '10', 'J', 'Q', 'K', 'A']
NAME_TO_VALUE = {n: i for i, n in enumerate(NOMINALS)} -> :<class 'dict'>: {'7': 0, '8': 1, '9': 2, '10': 3, 'J': 4, 'Q': 5, 'K': 6, 'A': 7}
list_cards = [('♥', '9'), ('♥', 'J'), ('♦', 'J'), ('♥', '7'), ('♥', '10'), ('♦', '10')]
def sort_hand():
# return list_cards.sort(key=lambda c: (NAME_TO_VALUE[c[0]], c[1]))
# return sorted(list_cards, key=lambda c: (NAME_TO_VALUE[c[0]], c[1]))
return sorted(list_cards)