What does $0 and $1 mean in Swift Closures?

aashish tamsya picture aashish tamsya · Mar 22, 2016 · Viewed 45.6k times · Source
let sortedNumbers = numbers.sort { $0 > $1 }
print(sortedNumbers)

Can anyone explain, what $0 and $1 means in swift?

More Sample

array.forEach {
    actions.append($0)
}

Answer

AdamPro13 picture AdamPro13 · Mar 22, 2016

$0 is the first parameter passed into the closure. $1 is the second parameter, etc. That closure you showed is shorthand for:

let sortedNumbers = numbers.sort { (firstObject, secondObject) in 
    return firstObject > secondObject
}