Sort a Deck of Cards by Suit, Then by Rank
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
You are given an array of strings `deck`, where each string represents a playing card. Each card is encoded as a 2-character string: the first character is the rank and the second character is the suit.
Ranks, in increasing order, are `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `T` (10), `J` (Jack), `Q` (Queen), `K` (King), `A` (Ace). Suits, in increasing order, are `C` (Clubs), `D` (Diamonds), `H` (Hearts), `S` (Spades).
Return the deck sorted first by suit (Clubs, Diamonds, Hearts, Spades), and then by rank within each suit in increasing order.
### Function Signature
```python
def sort_deck(deck: list[str]) -> list[str]:
```
### Rules
- Ace is high: within a suit, `A` comes after `K`.
- The deck can hold the same card more than once, since it can be longer than 52 cards. Copies of a card are identical strings and end up next to each other in the output.
### Constraints
- `1 <= len(deck) <= 10^4`
- Each card is a 2-character string. The first character is a rank from `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `T`, `J`, `Q`, `K`, `A`.
- The second character is a suit from `C` (clubs), `D` (diamonds), `H` (hearts), `S` (spades).
### Examples
```text
Input: deck = ["AS", "2C", "KH", "TD", "5S", "3C", "JH", "9D"]
Output: ["2C", "3C", "9D", "TD", "JH", "KH", "5S", "AS"]
```
Grouped by suit in the order C, D, H, S and then by rank, the groups are clubs [2C, 3C], diamonds [9D, TD], hearts [JH, KH] and spades [5S, AS].
```text
Input: deck = ["7H", "7C", "7S", "7D"]
Output: ["7C", "7D", "7H", "7S"]
```
All cards have rank 7, so only the suit order C, D, H, S decides the result.
```text
Input: deck = ["QH", "AS", "KH", "2S", "TS", "AS"]
Output: ["QH", "KH", "2S", "TS", "AS", "AS"]
```
Hearts come before spades. Within hearts the Queen ranks below the King, and within spades the order is 2, T (10), then both copies of the Ace.
Overview: Sort a list of two-character playing card codes by suit and then by rank, where the ranks include the letters T, J, Q, K and A. It tests building a composite sort key from custom orderings and spotting where plain string ordering gives the wrong result.
You are given an array of strings `deck`, where each string represents a playing card. Each card is a 2-character string: the first character is the rank and the second character is the suit.
Ranks, in increasing order, are `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `T` (10), `J` (Jack), `Q` (Queen), `K` (King), `A` (Ace). Suits, in increasing order, are `C` (Clubs), `D` (Diamonds), `H` (Hearts), `S` (Spades).
Implement `sort_deck(deck)`, which returns the cards of `deck` sorted first by suit (Clubs, Diamonds, Hearts, Spades), and then by rank within each suit in increasing order.
### Rules
- Ace is high: within a suit, `A` comes after `K`.
- The deck can hold the same card more than once, since it can be longer than 52 cards. Copies of a card are identical strings and end up next to each other in the output.
- The output holds exactly the cards of `deck`, with every copy kept.
### Examples
```text
Input: deck = ["AS", "2C", "KH", "TD", "5S", "3C", "JH", "9D"]
Output: ["2C", "3C", "9D", "TD", "JH", "KH", "5S", "AS"]
```
Grouped by suit in the order C, D, H, S and then by rank, the groups are clubs [2C, 3C], diamonds [9D, TD], hearts [JH, KH] and spades [5S, AS].
```text
Input: deck = ["QH", "AS", "KH", "2S", "TS", "AS"]
Output: ["QH", "KH", "2S", "TS", "AS", "AS"]
```
Hearts come before spades. Within hearts the Queen ranks below the King, and within spades the order is 2, T (10), then both copies of the Ace.
### Constraints
- `1 <= len(deck) <= 10^4`
- Each card is a 2-character string. The first character is a rank from `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `T`, `J`, `Q`, `K`, `A`.
- The second character is a suit from `C` (clubs), `D` (diamonds), `H` (hearts), `S` (spades).
- The same card can appear more than once.
Constraints
- 1 <= len(deck) <= 10^4
- Each card is a 2-character string. The first character is a rank from 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A.
- The second character is a suit from C (clubs), D (diamonds), H (hearts), S (spades).
- The same card can appear more than once.
Examples
Input: (['5H'],)
Expected Output: ['5H']
Explanation: Minimum valid deck: a single card comes back unchanged.
Input: (['AS', '2C', 'KH', 'TD', '5S', '3C', 'JH', '9D'],)
Expected Output: ['2C', '3C', '9D', 'TD', 'JH', 'KH', '5S', 'AS']
Explanation: First source example: two cards per suit, grouped C, D, H, S and ordered by rank inside each group.
Hints
- The suit decides the order before the rank does, so a high card in an earlier suit still comes before a low card in a later suit.
- Character codes put A, J, K, Q and T in a different order from the card ranks, so compare ranks by their place in the list 2 to A.
- Copies of the same card are identical strings, so their order among themselves never affects the answer.