TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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()

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)

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.


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.


@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


See -> Sorting Rules


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.