UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 9 - How to create and use closures: Example confusion

Forums > 100 Days of SwiftUI

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.

2      

Hi! I think you are trying to grasp everything at once. Even if you see what apple implements behind the scene you will not understand immediately what is going on. Apple does not show directly algorithm they implement. What you need to understand at this current stage is the logic of this method sorted(by:) which accepts two parameters and returns Bool . If you press enter after sorted(by:. You will see the parameters accepted and what this method returns <(String, String) throws -> Bool>. So when you provide array for sorting, this algorithm takes name1 and name2 and compares them. By default it compares them by ascending order. In case name1 preceeds name2 it returns true and continues comparing other elements. As your func captainFirstSorted(name1: String, name2: String) -> Bool is of the same type i.e. accepts two strings and return bool you can use it in your sorted(by:) method. The only additional condition in that function is to make sure "Suzanne" is placed first i.e. condition is true. Other elements will be placed in ascending order and with strings alphabetically. You don't really need to know what is going on under the hood. To certain extent you may find useful to read documentation on that. And even there you will not find what is going on under the hood.

2      

@Gnat brings up fond memories!

I still don't understand how the function "captainFirstSorted"
causes the sorted() function to know that it should sort Suzanne first.

See -> Sorting Suzanne

or

See -> Sorting Rules

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.