Sort a Deck of Cards by Suit, Then by Rank

Quick 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.

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.

General logo
General
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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

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

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].

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.

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.