I have an array of strings (of an app's versions) randomly ordered like:
var array = ["2.12.5", "2.12.10", "2.2", "2.11.8"]
The sorted array should be ["2.12.10", "2.12.5", "2.11.8", "2.2"] (ordered by most recent). I am trying to sort it.
//Compare by string
array.sort { //This returns ["2.2", "2.12.5", "2.12.10", "2.11.8"]
$0 > $1
}
//Compare by int
array.sort { //This returns ["2.12.10", "2.12.5", "2.11.8", "2.2"]
(Int($0.replacingOccurrences(of: ".", with: "")) ?? 0) > (Int($1.replacingOccurrences(of: ".", with: "")) ?? 0)
}
None of this is working properly. What is the best way to return the correct array?