Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Closures Closures and Closure Expressions Using the Sorted Function

David Bauer
David Bauer
3,863 Points

Using the Sorted Function gives an error

I'm working with Code 7 Beta and Swift 2.0 This is my code:

var names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]

func backwards(s1: String, s2: String) -> Bool { return s1 > s2 }

let sortedNames = sorted(names, {(s1: String, s2: String) -> Bool in return s1 > s2})

XCode gives me an error that says: "sorted" is unavailable: call the "sort()" method on the collection.

So my question is how do I make this happen? Any help is appreciated!

2 Answers

Piplup Pochama
Piplup Pochama
16,851 Points

Looks like Xcode's error notifications haven't been keeping up with Swift 2.0 changes.

Here's the Swift 2.0 solution for Pasan's first example:

var names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]

func backwards(s1: String, s2: String) -> Bool {
    return s1 > s2
}

names.sort(backwards)

and the second:

let sortedNames = names.sort({(s1: String, s2: String) -> Bool in return s1 > s2})
David Bauer
David Bauer
3,863 Points

Ah, that worked. Thank you!

Worked perfect here.

Thanks

hi Piplup Pochama here is my code i tried to do as what you wrote here but it still ask me to fix my code var names = ["Nour" , "ara" , "Maha" , "Joud"] func whatSouldComeFirst (S1: String ,S2: String) -> Bool {

return S1 > S2

} names.sort(whatSouldComeFirst) let sortedNames = names.sort{(S1: String ,S2: String)->Bool in return S1 > S2})

this is my code and it still ask me to fix it by adding a "; " at the end !!

David Bauer
David Bauer
3,863 Points

Well, if I use you code Xcode gives me another error says: "Passing value of type "[String]" to an inout parameter requires explicit "&"."
Under "Fix-It" Code recommends to add a "&" to "names" so that the code looks like this:

            let sortedNames = sort(&names, {(s1: String, s2: String) -> Bool in return s1 > s2})

If I do this I get another error that says: ""sort" is unavailable: call the "sortInPlace()" method on the collection." So I do this:

            let sortedNames = sortInPlace(&names, {(s1: String, s2: String) -> Bool in return s1 > s2})

...it gives me another error says: "Use of unresolved identifier "sortInPlace".

I don't know what do here :(

Roberta Voulon
Roberta Voulon
5,792 Points

Hi David,

When you need to "call method 'sort()' on the collection 'names'", this is what that looks like in code:

names.sort()

What you did instead is this:

sort(names)

...which translates back as "pass the collection 'names' to the function 'sort()'" and that's not the same thing.

Does this make sense to you?

There have been quite a few changes in Swift 2.0, it's a challenge but it keeps you on your toes!