Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Map Sorted Closure

I am trying to understand closures in swift and am missing something fundamental.
Given the following example:

var numbers = [20, 19, 7, 12]
numbers.map({(number: Int) -> Int in return 3 * number})
numbers.map {(number: Int) -> Int in return 3 * number}
numbers.sorted {(n1:Int, n2:Int) -> Bool in return n1 < n2}

Why do the extra braces work for map but not sorted? i.e.

numbers.sorted({(n1:Int, n2:Int) -> Bool in return n1 < n2})

does not compile...

"error: argument passed to call that takes no arguments"

Please could someone could explain the difference? Thanks.

like image 665
Duncan Rowland Avatar asked Nov 20 '16 09:11

Duncan Rowland


People also ask

What is Compactmap in Swift?

Returns an array containing the non- nil results of calling the given transformation with each element of this sequence.

What is the difference between sort and sorted in Swift?

sorted() and sorted(by:) has the same functionality as sort() and sort(by:) . The only difference is that they return the new sorted elements of the sequence instead of modifying the original array.

How does Swift sorted work?

Strings in Swift conform to the Comparable protocol, so the names are sorted in ascending order according to the less-than operator ( < ). To sort the elements of your sequence in descending order, pass the greater-than operator ( > ) to the sorted(by:) method. The sorting algorithm is not guaranteed to be stable.

How do you sort an array in ascending order in Swift?

To sort the array we use the sort() function. This function is used to sort the elements of the array in a specified order either in ascending order or in descending order. It uses the “>” operator to sort the array in descending order and the “<” operator to sort the array in ascending order.


2 Answers

Because sort method is func sorted(by:). So you have to add by param name:

numbers.sorted(by: {(n1:Int, n2:Int) -> Bool in return n1 < n2})
like image 127
Avt Avatar answered Oct 24 '22 01:10

Avt


sorted():

A sorted array of the collection’s elements.

Means it returns a new -sorted- array, which it should assigned to a new instance. The simple way to implement it is:

let numbers = [20, 19, 7, 12]
let sortedNumbers = numbers.sorted { $0 < $1 } // [7, 12, 19, 20]

If you need to sort the array itself, use sort() instead:

var numbers = [20, 19, 7, 12]
numbers.sort { $0 < $1 }

print(numbers) // [7, 12, 19, 20]

Note: when using sort(), make sure that numbers array is var (mutable).

The same behavior is also appiled to map(_:):

Returns an array containing the results of mapping the given closure over the sequence’s elements.

You can also implment the map() in a simpler way:

let numbers = [20, 19, 7, 12]

let mappedNumbers = numbers.map { $0 * 3 }

print(mappedNumbers) // [60, 57, 21, 36]

If you want to map the array itself, you should implement:

var numbers = [20, 19, 7, 12]
numbers = numbers.map { $0 * 3 }

print(numbers) // [60, 57, 21, 36]

Or in a single line:

let numbers = [20, 19, 7, 12].map { $0 * 3 }

print(numbers) // [60, 57, 21, 36]
like image 42
Ahmad F Avatar answered Oct 24 '22 01:10

Ahmad F