Hi everyone,
I'm currently on day 9(Creating and using closures) and I am having trouble understanding the example he gave below:
let team = ["Gloria", "Suzanne", "Piper", "Tiffany", "Tasha"]
let sortedTeam = team.sorted()
print(sortedTeam)
func captainFirstSorted(name1: String, name2: String) -> Bool {
if name1 == "Suzanne" {
return true
} else if name2 == "Suzanne" {
return false
}
return name1 < name2
}
let captainFirstTeam = team.sorted(by: captainFirstSorted)
print(captainFirstTeam)
I still don't understand how the function "captainFirstSorted" causes the sorted() function to know that it should sort Suzanne first.
How is the value assigned to name1 and name2?
Why isn't there one parameter for each item in the array?
If name1 == "Suzanne" how does function know to put "Suzanne" before the rest?
What happens if the function returns false? How does it still produce the intended result if it doesn't fulfill the condition for it to be true? Does it work like a loop?
In scenario 3, where "Suzanne" is neither name1 or name2, how does it still know it should put "Suzanne" first?
I'm sorry if this post is really messy but I have been having a really hard time trying to wrap my head around this.