Hi there,
Chugging along and have come to a screeching hault while I learn closures.
The team sorting by captain has given me a headache.
In this example:
let team = ["Gloria", "Suzzane", "Piper", "TIffany", "Tasha"]
let sortedTeam = team.sorted()
print (sortedTeam)
func captainFirstSorted(name1: String, name2: String) -> Bool {
if name1 == "Suzzane"{
return true
} else if name2 == "Suzzane" {
return false
}
return name1 < name2
}
let captainFirstTeam = team.sorted(by: captainFirstSorted)
print(captainFirstTeam)
How does the captainfirstSorted
function know that name1
and name2
are first and second values of the array team
?
I assume it just plucks out the first and second values of the array but I don't understand how this works as I would expect comparing each name to a position in the array (team[x]
) as shown below:
func captainFirstSorted() -> Bool {
if team[0] == "Suzzane"{
return true
} else if team[1] == "Suzzane" {
return false
}
return team[0] < team[1]
}
Thank you for any help you can provide!! :)