2

I need to order an array of struct.

I've try:

let aRes =  self.aSoundTracks_Filtered.sort{ $0.st < $1.st }

provide error: Cannot invoke 'sort' with an argument list of type '((_, _) -> _)'

also try this:

let hasPrefixAndSuffixw =  self.aSoundTracks_Filtered.sort( $0.st < $1.st )

provide error: Anonymous closure argument not contained in a closure

Any idea? :)

My aSoundTracks_Filtered was delared like this:

   var aSoundTracks_Filtered = [SoundTrack]()

My struct was like this:

struct SoundTrack {
    let sID : Int
    let st : String
}
6
  • What I'm assuming aSoundTracks_Filtered is your array? What type does it contain, can you provide the code for that? Commented Jul 17, 2015 at 18:33
  • 1
    Looks like a type inference problem, try {(a, b) -> Bool in a.st < b.st} Commented Jul 17, 2015 at 18:35
  • I have added info to the question. Hope it help Commented Jul 17, 2015 at 18:44
  • oisdk: let hasPrefixAndSuffix = self.aSoundTracks_Filtered.sort {(a, b) -> Bool in a.st < b.st} get error: Cannot find an overload for 'sorted' that accepts an argument list of type '([(SoundTrack)], (_, _) -> _)' Commented Jul 17, 2015 at 18:53
  • Was i Type, works now. Thanks :) Commented Jul 17, 2015 at 19:09

2 Answers 2

9

Your code works fine when you tested in a Playground in the following way:

struct SoundTrack {
   let sID : Int
   let st : String
}

var aSoundTracks_Filtered = [SoundTrack]()

aSoundTracks_Filtered.append(SoundTrack(sID: 1, st: "a"))
aSoundTracks_Filtered.append(SoundTrack(sID: 2, st: "b"))

aSoundTracks_Filtered.sort{ $0.st > $1.st } // [{sID 2, st "b"}, {sID 1, st "a"}]

But sort() sorts an array in-place. What you probably want to use is sorted(), which does not modify the original array and returns a new sorted array:

let aRes = aSoundTracks_Filtered.sorted{ $0.st > $1.st }

The above code is for Swift 1.2, for Swift 2.0 returning a sorted array is called "sort" again, but it is a (protocol extension) method now instead of a global function. I hope this help you.

Sign up to request clarification or add additional context in comments.

3 Comments

your 2 suggestions return Cannot invoke 'sort' with an argument list of type '((_, _) -> _)' :(
I tested using Xcode 6.4 and Swift 1.2 and it works fine.
thank you!...works for me also with slight modification
-1

So, it's actually pretty simple. However I wonder where the selfcomes from when you access the array. I don't know which class it belongs to, in case it would belong to the struct itself (I wouldn't know why but just in case) you'll have to mark the function as mutating as you're changing the value of a struct's attribute. The second thing is actually that you'll have to use curley brackets:

self.aSoundTracks_Filtered.sort({$0.st < $1.st})

4 Comments

aSoundTracks_Filtered is an array not a function, see declaration, the use of parentheses is optional you can call sort without it, see my answer
I'm certainly aware of that - what you are doing is using Trailing Closures so in fact yes you can eliminate the parantheses. However it's certainly not appropriate to cast the value to an Implicitly Unwrapped Optional as I've seen in your answer before
As you have see in my last answer the explicit cast is not optional you can read more about it in my answer here stackoverflow.com/questions/30040996/…
Your other answer alludes to the fact that you have to unwrap optionals as subscripting a dictionary returns an optional - totally different topic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.