In this line, you are comparing strings.
The string “card11” (i.e., a Jack) is not greater than the string “card4” because strings are not compared the same as integers.
let playerInt = 11
let cpuInt = 4
print(playerInt > cpuInt)
//prints true
let playerCard = "card11"
let cpuCard = "card4"
print(playerCard > cpuCard)
//prints false
With integers, 11
is obviously greater than 4
.
With strings, you aren’t workinng with the numbers 11
and 4
, you are working with the characters "11"
and "4"
. Since the character "1"
sorts before the character "4"
, then "11"
comes first. Numbers (like Int
s) are sorted numerically; strings are sorted lexicographically.
Run this code in a playground and you will see what I mean:
let rng = 1...14
//sorted range of Ints
print(rng.sorted())
//prints [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
//sorted range of strings
print(rng.map { "\($0)" }.sorted())
//prints ["1", "10", "11", "12", "13", "14", "2", "3", "4", "5", "6", "7", "8", "9"]