Hi, I've been stuck on trying to sort a hand and while I've tried to do a bubble sort, it doesn't seem to work. Here's my code:
def rankValue(card): #Converts card value to integer (rank)
if card.value == "Ten":
return 10
elif card.value == "Jack":
return 11
elif card.value == "Queen":
return 12
elif card.value == "King":
return 13
elif card.value == "Ace":
return 14
else:
return int(card.shortValue[0])
def rankSuit(card): #Converts card suit to integer (rank)
if card.suit == "Spades":
return 3
elif card.suit == "Hearts":
return 2
elif card.suit == "Diamonds":
return 1
else:
return 0
def sortHand(hand):
for i in range(len(hand)-1):
valueHand = valueRank(hand[i]) #converts value to rank
suitHand = suitRank(hand[i]) #converts suit to rank
for j in range(len(hand)-1):
if hand[i] > hand[i+1]:
temp = hand[i]
hand[i] = hand[i+1]
hand[i+1] = temp
return hand
print "Player Hand: ", playerHand
print sortHand(playerHand)
For example, I want to sort:
[3C, 9D, 5C, KC, TS]
to
[3C, 5C, 9D, TS, KC]
where I sort the values first before the suits. This is where my main issue lies:
for j in range(len(hand)-1):
if hand[i] > hand[i+1]:
temp = hand[i]
hand[i] = hand[i+1]
hand[i+1] = temp
I don't know how to implement valueHand/suitHand to that code since when I convert the values to their ranks, it is no longer a list