Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2 sorting - Cannot invoke 'sort' with an argument list of type…

Tags:

sorting

swift2

I have a class, say Penguin

class Penguin {
   var beakLength: Float
}

Trying to sort an array of Penguins as follows:

let penguins = [Penguin]()
let sortedPenguins = penguins.sort { $0.beakLength < $1.beakLength }

gives error message:

Cannot invoke 'sort' with an argument list of type '(@noescape (Penguin, Penguin) -> Bool)'

Expected an argument list of type '(@noescape (Self.Generator.Element, Self.Generator.Element) -> Bool)'

What am I missing here?

like image 602
Ashley Mills Avatar asked Nov 02 '15 13:11

Ashley Mills


1 Answers

For anyone who has the same problem, it turns out the code sample above wasn't quite correct. What was actually more like:

var sortedPenguins = [Chimp]()

let penguins = [Penguin]()
sortedPenguins = penguins.sort { $0.beakLength < $1.beakLength }

D'oh!

like image 167
Ashley Mills Avatar answered Oct 02 '22 15:10

Ashley Mills