mark_priority = {'D' => 1, 'C' => 2, 'H' => 3, 'S' => 4} number_priority = {} (2..9).each { |e| number_priority[e.to_s] = e } number_priority.merge!({'A' => 1, 'T' => 10, 'J' => 11, 'Q' => 12, 'K' => 13}) gets cards = gets.chomp.split puts cards.sort_by { |card| number_priority[card[1]] }.sort_by { |card| mark_priority[card[0]] }.join(' ')