On Syntactic Sugars. What's the Long Version of This?

Hi. On syntactic sugar (which is what it's called when things are replace with the $ sign, I believe).

What's the long version of this (meaning what does $0 and $1 represent)?


var numbers = [29, 19, 7, 12]


let sortedNumbers = numbers.sorted()

{ $0 > $1 }



Thank you in advance.

God bless, Genesis 1:3

Accepted Reply

Before going on to long version, first, short version.

let sortedNumbers0 = numbers.sorted{ $0 > $1 }


A little long version:

let sortedNumbers2 = numbers.sorted(by: { $0 > $1 })


Long version:

let sortedNumbers3 = numbers.sorted(by: { (left: Int, right: Int)->Bool in left > right })


In a closure expression, `$0` represents the first argument passed to the closure and `$1` represents the second.

(See Shorthand Argument Names in the linked article.)


(Seems this is a topic for the Swfit area.)

Replies

Before going on to long version, first, short version.

let sortedNumbers0 = numbers.sorted{ $0 > $1 }


A little long version:

let sortedNumbers2 = numbers.sorted(by: { $0 > $1 })


Long version:

let sortedNumbers3 = numbers.sorted(by: { (left: Int, right: Int)->Bool in left > right })


In a closure expression, `$0` represents the first argument passed to the closure and `$1` represents the second.

(See Shorthand Argument Names in the linked article.)


(Seems this is a topic for the Swfit area.)