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

Closures and the sorted function

Forums > 100 Days of SwiftUI

Hey im new to the course and while playing around with the code from the closures section and where paul introduces the shorthand $0 and $1 and so on i stumbled on something. I first saw it in some large sorting code i wrote but i boiled it down to this:

var firstArray = [9,3,6,]
let finalArray = firstArray.sorted{
    print("$0 is \($0) and $1 is \($1)")
    print($0<$1)
    return $0 < $1 }

print(finalArray)

This is an int array where i am sorting it in ascending order. It was my understanding from the video that the shorthand $0 means the first value from the array and that $1 is then the value after. And we want the first value $0 to be less than $1 which is what the code $0<$1 says.

But the code in the print function on the first run through says that $0 is 3 and $1 is 9. And the 2nd print return true true false when i should expect it to return false for the first print.

But the code works exactly as it should though so clearly i dont understand something.

I hope any 1 of you can explain this to me and thank you in advance to anyone who takes the time :)

1      

$0 and $1 refer to the first and second parameters passed to the closure, not necessarily to the first and second items in the array. The sorted(by:) API doesn't guarantee that the array items are passed into the closure in the same order they appear in the array.

1      

https://www.hackingwithswift.com/forums/100-days-of-swiftui/closures-and-the-sorted-function/16646/16647 Hey @roosterboy thank you for replying. I did read once that, that might be the case but some experimentation left me in doubt.

I have found it to be the case for literally all the different arrays i have made, consisting of single letters like ["a", "e","d","b","c"] or ints [2,6,7,1,4] or even the array of names from the video ["Gloria", "Suzanne", "Piper", "Tiffany", "Tasha"] to all show this behaviour where the 2nd value is always stored in $0 and the 1st in $1. Which seems to suggest that some sort of behaviour is guaranteed?

I have gotten an answer earlier suggesting that it is simply the case that $0 is the 2nd value and $1 is the first just because, but they werent able to elaborate other than that.

1      

Which seems to suggest that some sort of behaviour is guaranteed?

Nope. Unless it is explicitly noted somewhere (online API docs, header files, etc) it is not guaranteed and should not be relied upon to always be that way.

And, really, does it matter? As long as the end result of a call to sorted(by:) is a sorted array, then what order the elements are passed into the closure is an implementation detail and isn't important.

All your closure should care about is whether the two items it is passed are already sorted in increasing order or not. The rest of the array and what the original order of the two items was in that array are irrelevant.

1      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

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.