Enfoque orientado a objetos
Es una buena práctica hacer que la lógica de clasificación de objetos, si corresponde, sea una propiedad de la clase en lugar de incorporarse en cada caso en el que se requiere el orden.
Esto garantiza la coherencia y elimina la necesidad de código repetitivo.
Como mínimo, debe especificar __eq__y las __lt__operaciones para que esto funcione. Entonces solo úsalo sorted(list_of_objects).
class Card(object):
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __lt__(self, other):
return self.rank < other.rank
hand = [Card(10, 'H'), Card(2, 'h'), Card(12, 'h'), Card(13, 'h'), Card(14, 'h')]
hand_order = [c.rank for c in hand] # [10, 2, 12, 13, 14]
hand_sorted = sorted(hand)
hand_sorted_order = [c.rank for c in hand_sorted] # [2, 10, 12, 13, 14]