paint-brush
Swift and JavaScript comparison snippets(7) — Closuresby@unbug
256 reads

Swift and JavaScript comparison snippets(7) — Closures

by unbugJuly 2nd, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Swift
featured image - Swift and JavaScript comparison snippets(7) — Closures
unbug HackerNoon profile picture

Swift













// Closure Expressions// The Sorted Methodlet names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]func backward(_ s1: String, _ s2: String) -> Bool {return s1 > s2}var reversedNames = names.sorted(by: backward)// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool inreturn s1 > s2})// orreversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in return s1 > s2 } )

JavaScript













// Closure Expressions// The Sort Methodconst names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]function backward(s1, s2) {return s1 < s2}let reversedNames = names.sort(backward)// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]reversedNames = names.sort((s1, s2) => {return s1 < s2})// orreversedNames = names.sort((s1, s2) => return s1 < s2)

More of Swift and JavaScript comparison snippets