Why does IF >< function doesn't work

Hi,

Can someone please help me with this issue, I am quite new in this, I am using Swift for 2 days now and I am facing an weird thing...If anyone can please explain me what is wrong in this code...the IF >< function doesn't work as it doesn't recognise the higher number...I will attach the picture here and also the code if you guys can please explain what I am doing wrong...I am trying to fix this for couple of hours and I get stuck here....Thanks in advance!

import UIKit

let leftNumber = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "02", "03", "04", "05", "06", "07", "08", "09", "010", "011", "012", "013", "014", "002", "003", "004", "005", "006", "007", "008", "009", "0010", "0011", "0012", "0013", "0014", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "00010", "00011", "00012", "00013", "00014"]

let randomName = leftNumber.randomElement()!

let rightNumber = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "02", "03", "04", "05", "06", "07", "08", "09", "010", "011", "012", "013", "014", "002", "003", "004", "005", "006", "007", "008", "009", "0010", "0011", "0012", "0013", "0014", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "00010", "00011", "00012", "00013", "00014"]

let randomName2 = rightNumber.randomElement()!

let trimmedString = randomName.replacingOccurrences(of: "^0+", with: "", options: .regularExpression)

let trimmedString2 = randomName2.replacingOccurrences(of: "^0+", with: "", options: .regularExpression)

if trimmedString > trimmedString2 {

print ("Number 1")

}

else if trimmedString2 > trimmedString {

print ("Number 2")

}

You're comparing Strings, not numbers, and "3" comes after "13", at least in Swift's default String sorting.

1 Like

Thank you very much for the fast response!!

So can you please give me an idea how to make this work? How can I make random numbers based on the numbers I have there?

I suggest you either look up how to customize String comparison or how to convert Strings to numbers.

1 Like

Thanks, will go back to practice now, it makes sense for me now!