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)

def rankSuit(card): #Converts card suit to integer (rank)
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

well simply write a bubble sort function in conjunction with my code:

``````# attach my code from the other post

def bubbleSortCards(listOfCards):
# implemented using the pseudocode from wikipedia
# ``````

## All 6 Replies

well simply write a bubble sort function in conjunction with my code:

``````# attach my code from the other post

def bubbleSortCards(listOfCards):
# implemented using the pseudocode from wikipedia
# http://en.wikipedia.org/wiki/Bubble_sort
while True:
swapped = False
for i in range(len(listOfCards)-1):
current = listOfCards[i]
nextOne = listOfCards[i+1]
if current > nextOne:
listOfCards[i] = nextOne
listOfCards[i+1] = current
swapped = True
break

if swapped == False:
break

return listOfCards

if __name__ == "__main__":
c_3S = Card(1, 3)
c_5S = Card(3, 3)
c_TC = Card(8, 1)
playerHand = [c_TC, c_5S, c_3S]
print "Bubble Sort: "
bubblesortedcards = bubbleSortCards(playerHand)
for card in bubblesortedcards:
print card``````

Are you sure you want to use bubble sort?

According to the Wikipedia article it is one of the worst sorting algorithms.

Yes if you need to reimplement the wheel, I would suggest that you choose Shell sort (improved insert sort), which is not so bad at all for small lists. Though for the case of 5 cards, the method is quite irrelevant as even checking all permutations for the sorted one is fast enough in modern computers. But if you care how code looks...

``````def shellSort(array):
"Shell sort using Shell's (original) gap sequence: n/2, n/4, ..., 1."
gap = len(array) // 2
# loop over the gaps
while gap > 0:
# do the insertion sort
for i in range(gap, len(array)):
val = array[i]
j = i
while j >= gap and array[j - gap] > val:
array[j] = array[j - gap]
j -= gap
array[j] = val
gap //= 2``````

I would rename array to something else, even implement it more pythonically, but here it is anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.