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

SOLVED: Question: about 'sorted(closure)`

Forums > 100 Days of SwiftUI

@boat  

Hi When Paul demonstrating closure , he used sorted (by: code of closures).

https://www.hackingwithswift.com/quick-start/beginners/how-to-use-trailing-closures-and-shorthand-syntax

let team = ["Gloria", "Suzanne", "Piper", "Tiffany", "Tasha"]

let captainFirstTeam = team.sorted(by: { (name1: String, name2: String) -> Bool in
    if name1 == "Suzanne" {
        return true
    } else if name2 == "Suzanne" {
        return false
    }

    return name1 < name2
})

print(captainFirstTeam)

Quetions: Does this mean sorted() is taking in the returned Bool generated by the closure as a parameter ?

How do I know what type of parameters sorted are meant to take in ? Or I could just pass in any type of parameters to sorted ?

Thank you in advance,

Boat

4      

sorted(by:) gives you two elements from the sequence and you return a Bool indicating if those two elements are in the correct order. The implementation of sorted(by:) then uses that result to sort the sequence.

You don't really need to know what type of parameters to pass in to sorted(by:); Swift takes care of that for you by passing it whatever type the sequence's Element is. (i.e., if your sequence is [String], the closure gets two Strings; if the sequence is [Int], the closure gets two Ints, etc) You also don't need to indicate the closure's return type.

let captainFirstTeam = team.sorted(by: { name1, name2 in
    return name1 < name2
})

Heck, you don't even need to name the parameters:

let captainFirstTeam = team.sorted(by: { 
    return $0 < $1
})

The closure syntax can be reduced even more than that, but that might be a good place to stop for now.

So, basically, if you look at sorted(by:)'s signature in the documentation:

func sorted(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> [Element]

you'll see that the closure takes two items that are whatever type the sequence's Elements are and returns a Bool.

5      

@boat  

Thank you @roosterboy !

3      

It helped me a lot to understand the "name1" and "name2" parameters when I put 'print' commands to see the action happening:

func captainFirstSorted(name1: String, name2: String) -> Bool {
    let captain = "Suzanne"
    if name1 == captain {
        print("\(name1) is being compared to \(name2), and \(name1) should come first")
        return true
    } else if name2 == captain {
        print("\(name1) should not be before \(name2)")
        return false
    }
    print("\(captain) is not being compared, so just sort \(name1) and \(name2)")
    return name1 < name2
}

5      

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.

Click to save your free spot now

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.