Search code examples
pythonfunctionsortingtuplesbubble-sort

Sorting list of tuples representing cards by alphabet and then rank


I want to sort a hand of cards by alphabetical order first (Club, Diamond, Heart, Spade) and then by rank (Ace to King). I have the following (very lengthy and inefficient) code but the output seems wrong. I feel like it is an issue with the bubble sort that I use. What am I missing here, and what would be a better way of doing this? For my final output I also will convert the 2, 11, 12, and 13 back to Ace, Jack, Queen and King but I really do not want to redo what I did to get them into a numerical value in the first place. The I/O should look like this:

Sample Input: [('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]

Sample Output: [('c', 'j'), ('d', '2'), ('d', '10'), ('d', 'j'), ('h', '5'), ('h', 'q'), ('s', '3')]

Here is the code I have so far and the resulting output:

def sort_hand(hand):
    """
    Sorts the a hand of cards by the value of each suit/type of card. 
    """
    
    # A=1,2,3,4,5,6,7,8,9,10,J=11,Q=12,K=13
    # UPDATING CARDS SO THAT THEY CAN BE ORDERED BY RANK
    new_hand = []
 
    # GIVING NUMERIC VALUES TO JACK, QUEEN, KING, AND ACE
    for element in hand: 
        if element[1] == 'j':
            val = element[1].replace('j', '11')
            new_element = (element[0], val)
            new_hand.append(new_element)
        elif element[1] == 'q':
            val = element[1].replace('q', '12')
            new_element = (element[0], val)
            new_hand.append(new_element)
        elif element[1] == 'k':
            val = element[1].replace('k', '13')
            new_element = (element[0], val)
            new_hand.append(new_element)
        elif element[1] == 'a':
            val = element[1].replace('a', '1')
            new_element = (element[0], val)
            new_hand.append(new_element)
        else:
            pass
            new_hand.append(element)
    
    # BUBBLE SORT USED TO ORDER BY RANK 
    for ix in range(1, len(new_hand)):
        value_to_sort = new_hand[ix][1]

        while new_hand[ix-1][1] > value_to_sort and ix > 0:
            new_hand[ix], new_hand[ix-1] = new_hand[ix-1], new_hand[ix]
            ix -= 1
            
    # MAKE SUBLISTS FOR EACH SUIT      
    c_list = []
    d_list = []
    h_list = []
    s_list = []
            
    for element in new_hand:
        if element[0] == 'c':
            c_list.append(element)
        elif element[0] == 'd':
            d_list.append(element)
        elif element[0] == 'h':
            h_list.append(element)
        else: 
            s_list.append(element)
    
    # COMBINE ORDERED SUIT SUBLISTS TO MAKE FINAL SORTED HAND BY RANK
    final_hand = c_list + d_list + h_list + s_list
            
    return final_hand

>>> hand = [('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]  
>>> sort_hand(hand)
[('c', '11'),
 ('d', '10'),
 ('d', '11'),
 ('d', '2'),
 ('h', '12'),
 ('h', '5'),
 ('s', '3')]

I feel like I am close with this code, but not sure where to go from here. Would appreciate a well-explained answer, thank you.


Solution

  • Use lambda to sort with a dual key.

    rank = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'q', 'k', 'a']
    suit = ['c', 'd', 'h', 's']
    
    inp = [('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]
    
    outp = sorted(inp,key = lambda x: 
        (suit.index(x[0]), rank.index(x[1])))
    
    print(outp)
    

    Output:

    [('c', 'j'), ('d', '2'), ('d', '10'), ('d', 'j'), ('h', '5'), ('h', 'q'), ('s', '3')]