I have a class list containing objects. I want to sort the class list based on two of the attributes of the objects. Some of the attributes will be of equal value, in which case I want to sort them based on a secondary attribute. How would I do this? My stuff looks like
f.cards
Out[95]:
[<__main__.PlayingCard at 0x56d44e0>,
<__main__.PlayingCard at 0x56d4438>,
<__main__.PlayingCard at 0x56d4588>,
<__main__.PlayingCard at 0x56d4390>,
<__main__.PlayingCard at 0x56d4828>,
<__main__.PlayingCard at 0x56d4400>,
<__main__.PlayingCard at 0x56d4358>]
f.cards[0].give_value()
Out[96]: 14
f.cards[0].getSuit()
Out[97]: 'Diamonds'
f.cards[2].give_value()
Out[100]: 14
f.cards[2].getSuit()
Out[101]: 'Hearts'
My intent is to define a sorting function, so that I can sort the cards by primarily their value, and secondarily their suit. Such as the example printout, where I would like the card with value 14 to be placed first as it has the suit 'Hearts'
My last attempt was this:
elements = sorted([(o.give_value(), o.suit) for o in cards], reverse=True)