Instead of sorting, which is O (n log (n)) for a good sort, use max(by:), which is O (n) on an Array, providing it with a closure to compare string lengths:
Swift 4:
Swift 4 count String:
let array = ["I'm Roi","I'm asking here","Game Of Thrones is just good"]
if let max = array.max(by: {$1.count > $0.count}) {
print(max)
}
Swift 3:
.characters.count String, :
let array = ["I'm Roi","I'm asking here","Game Of Thrones is just good"]
if let max = array.max(by: {$1.characters.count > $0.characters.count}) {
print(max)
}
Swift 2:
maxElement , :
let array = ["I'm Roi","I'm asking here","Game Of Thrones is just good"]
if let max = array.maxElement({$1.characters.count > $0.characters.count}) {
print(max)
}
: maxElement - O (n). - O (n log (n)), , .