Geoffrey Setiawan
Geoffrey Setiawan

Reputation: 57

Explanation of sorted(by:) in Swift

I am a little confused regarding the logic behind the sorted(by:) function in Swift (it was sort() in Swift 2). Take the code below for instance...how does the return type bool yield in the reverse ordering of the numbers?

let aBunchOfNumbers = [1,2,3,4,5,6,7,8,9,10]
let reverseSortClosure: (Int, Int) -> Bool = {
     (numberOne: Int, numberTwo: Int) -> Bool in 
     if numberOne < numberTwo {
         return true
     }
     return false
}

aBunchOfNumbers.sorted(by: reverseSortClosure)

Upvotes: 0

Views: 974

Answers (4)

kevchoi
kevchoi

Reputation: 434

(sort(_:) was renamed sorted(by:) since Swift 3)

From the docs: https://docs.swift.org/swift-book/LanguageGuide/Closures.html#ID95

The sorted(by:) method accepts a closure that takes two arguments of the same type as the array’s contents, and returns a Bool value to say whether the first value should appear before or after the second value once the values are sorted. The sorting closure needs to return true if the first value should appear before the second value, and false otherwise.

That is, the type class for the function that sorted() accepts looks like (T, T) -> Bool, where T is the type of the inout array you want to sort. The function returns true IF the first value of the function should appear before the second value. Swift uses this function to order the list you pass in.

Upvotes: 1

GoZoner
GoZoner

Reputation: 70215

Perhaps you are learning about closures, but you don't need one here. The sort() function takes a second argument that is a function to compare two values - the function returns true if its first argument appears before the second argument.

In your case, the array contains Int - there is already a perfectly good function to compare Ints - it is >. Use it like this:

 1> let result = [1,2,3,4].sort(>) 
result: [Int] = 4 values {
  [0] = 4
  [1] = 3
  [2] = 2
  [3] = 1
}

Upvotes: 0

Leo Dabus
Leo Dabus

Reputation: 236478

In Swift 2 the sort() method needs to be used as an extension of your array. aBunchOfNumbers.sort(reverseSortClosure) and if you want to reverse it you have to use >.

let aBunchOfNumbers = [1,2,3,4,5,6,7,8,9,10]
let reverseSortClosure: (Int, Int) -> Bool = {
    (lhs, rhs) -> Bool in
    return lhs > rhs
}

let reversed = aBunchOfNumbers.sort(reverseSortClosure)   // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

You can also simplify your code as follow:

let reversed = aBunchOfNumbers2.sort(>)

Upvotes: 1

jbcd13
jbcd13

Reputation: 143

The sort method is sorting the array, so the method needs to know what value goes first, second, third, etc. The value that goes first is the value that is true, and the value which goes second is false. That is why the sort method returns in a bool. Also, your code is really complicated and not at all swifty. This does the same thing, just a lot faster.

let aBunchOfNumbers = [1,2,3,4,5,6,7,8,9,10]
aBunchOfNumbers.sort({
$0 > $1 })

Don't be worried if you're not getting closures at first. They are difficult to understand and the syntax is somewhat unique to swift.

Hope this helps

Upvotes: 0

Related Questions